Practice Examples for Salesforce APEX Triggers

Here, you can find enough examples to practice on Triggers. Implement these scenarios in your dev org one by one and in no time, coding Triggers would be a piece of cake for you.

Before starting with the examples, I would request you to go through the Apex Best Practices so that right from the start you code efficiently.

  • Trigger – Example 1 : Write a trigger, when a new Account is created then create a contact related to that account.
  • Trigger – Example 2 : Write a trigger, if the owner of an account is changed then the owner for the related contacts should also be updated.
  • Trigger – Example 3 : Write a trigger, to create new Opportunity whenever an account is created/updated for Industry – Agriculture.
  • Trigger – Example 4 : Write a trigger, user should not be able to delete any account with associated contact.
  • Trigger – Example 5 : Write a trigger, only admin can delete closed opportunities and email should be sent to owners for closed won opportunities.
  • Trigger – Example 6: Write a trigger, to have the count of total number of contacts associated to an account.
  • Trigger – Example 7: Write a trigger, whenever the BillingCity of any Account is updated, update MailingCity of all the Contacts related to the account.
  • Trigger – Example 8: Write a trigger, whenever a new active User having profile “System Administrator” is inserted, add the user to the public group “Admins”.
  • Trigger – Example 9: Update Example 8, whenever user’s profile is changed from/to “System Administrator” AND user is marked active/inactive, add/delete the user to the public group “Admins”.
  • Trigger – Example 10: Write an Apex trigger named “OpportunityTrigger” that automatically updates a custom field named “TotalOpportunityAmount” on the Account object whenever an Opportunity related to that Account is closed/won.
  • Trigger – Example 11: Write an Apex trigger named “OpportunityValidationTrigger” that fires before update and enforces some conditions. If any of the conditions are violated, the trigger should add the appropriate custom error messages to the Opportunity record.

Leave a Reply