Apex Trigger Ten Examples
Apex Trigger Ten Examples
Problem: Before Delete on Account, prevent deletion if there are open opportunities related to
the account.
Problem: Make sure the Amount field is populated when the opportunity stage is set to
"Closed Won."
Problem: Before Insert or Before Update on Opportunity, ensure that the product quantity is
greater than 0.
Problem: Before Insert or Before Update on Opportunity, ensure the close date is not earlier
than a certain date.
Problem: Ensure that no two opportunities have the same name.(duplicate Opportunity by
name)
Problem: Write a trigger on lead to prevent duplicate records based on lead email, if a record
already created with the same Email, or record is Updated. The trigger should throw an error.
Problem: Write Apex trigger on Account to avoid creation of duplicate record if the account
with same phone exists in the system in Salesforce.
Scenario 8: Enforce the business logic for insertion (Related contact should not be more than
2)
Problem: Write a trigger on account. "An account should not have more than 2 contacts
associated with it"
Problem: Before insert on custom object, limit the number of records that user with specific
profile can create the record.