TRIGGERS IN APEX
1. What are Triggers?
o Triggers are blocks of Apex code (Salesforce's programming language)
that execute before or after specific events occur on your records.
o For example, you can write a trigger to do something when a record is
created, updated, deleted, or even undeleted.
2. When do Triggers run?
o Triggers run in response to these events:
Before Insert: Runs before a new record is saved to the
database.
After Insert: Runs after a new record is saved.
Before Update: Runs before an existing record is updated.
After Update: Runs after an existing record is updated.
Before Delete: Runs before a record is deleted.
After Delete: Runs after a record is deleted.
After Undelete: Runs after a deleted record is restored.
3. Why use Triggers?
o Triggers are used to automate tasks or enforce business rules. For
example:
Automatically updating a related record when a record is
created.
Preventing a record from being saved if certain conditions aren’t
met.
Sending an email notification when a record is updated.
Example of a Trigger:
Trigger Context Variables
These are special variables that provide information about the trigger event and the
records involved.
1. [Link]
o Contains the new versions of records being inserted or updated.
o Only available in before insert and before update triggers.
o Example: [Link] gives you the list of records being created or
updated.
2. [Link]
o Contains the old versions of records before they were updated or
deleted.
o Only available in after update, after delete, and after
undelete triggers.
o Example: [Link] gives you the list of records before they were
changed.
3. [Link]
o A map of the new versions of records, keyed by their record IDs.
o Useful for quickly accessing records by their ID.
o Example: [Link](recordId) gives you the updated
record.
4. [Link]
o A map of the old versions of records before they were updated or
deleted, keyed by their record IDs.
o Example: [Link](recordId) gives you the old version of the
record.
5. [Link]
o Returns true if the trigger is running due to an insert operation.
o Example: if ([Link]) { ... } checks if records are being
inserted.
6. [Link]
o Returns true if the trigger is running due to an update operation.
o Example: if ([Link]) { ... } checks if records are being
updated.
7. [Link]
o Returns true if the trigger is running due to a delete operation.
o Example: if ([Link]) { ... } checks if records are being
deleted.
8. [Link]
o Returns true if the trigger is running due to an undelete operation.
o Example: if ([Link]) { ... } checks if records are being
restored.
9. [Link]
oReturns true if the trigger is running before the records are saved to
the database.
o Example: if ([Link]) { ... } checks if the trigger is in the
"before" phase.
[Link]
o Returns true if the trigger is running after the records are saved to
the database.
o Example: if ([Link]) { ... } checks if the trigger is in the "after"
phase.
[Link]
o Returns the number of records involved in the trigger event.
o Maximum limit: 200 records.
o Example: [Link] tells you how many records are being processed.
[Link]
o Indicates the type of operation causing the trigger to run.
o Possible values:
BEFORE_INSERT, BEFORE_UPDATE, BEFORE_DELETE
AFTER_INSERT, AFTER_UPDATE, AFTER_DELETE, AFTER_UNDELE
TE
o Example: [Link] ==
[Link].BEFORE_INSERT checks if it’s a "before
insert" operation.
[Link]
o Returns true if the code is running in a trigger context (not in a
Visualforce page, web service, or anonymous Apex).
o Example: if ([Link]) { ... } checks if the code is running in
a trigger.
Quick Summary Table
Variable Description
[Link] New versions of records (insert/update).
[Link] Old versions of records (update/delete).
[Link] Map of new records by ID.
[Link] Map of old records by ID.
[Link] True if records are being inserted.
[Link] True if records are being updated.
[Link] True if records are being deleted.
[Link] True if records are being undeleted.
True if trigger is running before saving to the
[Link]
database.
[Link] True if trigger is running after saving to the
Variable Description
database.
[Link] Number of records in the trigger (max 200).
[Link] Type of operation
ype (e.g., BEFORE_INSERT, AFTER_UPDATE).
[Link] True if code is running in a trigger context.
Apex Trigger 1:
Develop an Apex Trigger so that every time any account is inserted then set
the value of the Industry field to Education if the Industry field is blank. Also,
check if the Description is blank then set the value for the description field to
“Account Description is blank”
Apex Trigger 1.2:
Develop an Apex Trigger so that every time when any account is created or
updated then Set the Value of the Billing Address is to Shipping Address.
Apex Trigger 1.3:
Develop an Apex Trigger so that every time when any account is created or
updated then Set the Value of the Billing Address is to Shipping Address.
You only need to update the Shipping Address information with Billing Address if
the Shipping Address (Compound Field) is having blank value.
Apex Trigger 2:
When the Account is Created, create a Task Record under that Account and
assign the Task to the Account Owner. Use the below information
Subject – Created from Apex Trigger
Comments – Created from Apex Trigger
Due Date –
Todays Date
+7
Status – Not
Started
Priority – High
Related To (What) – Account Id
Assigned To (OwnerId) – Account Owner Id
Click Here For Reference
Apex Trigger 3:(Using Handler, Helper and Dispatcher Class)
Reference: [Link]
Create a Custom field on Opportunity “Discount” and the data type of
this field should be percent.
Create a Custom field on Opportunity “Discounted Price” with
Currency Data Type
Develop an Apex Trigger on Opportunity so that if the Discount & Amount
field is not blank then calculate the discount and store it in the Discount
Price field.
To Calculate the discount use the below formula and store it in a variable
Decimal discount = ( Discount * Amount ) / 100
To Calculate the Discounted Price use the below calculation and store it in a
variable
Decimal discountedAmount = Amount – discount;
APEX TRIGGER 4: (Using Handler, Helper and Dispatcher Class)
Reference : [Link]
The business got to know that there are multiple accounts with the same
name and rating. Now, as a developer, you need to make sure that no new
duplicates are being created with the same name and rating.
APEX TRIGGER 5: (Using Handler, Helper and Dispatcher Class)
Reference : [Link]
Develop a Solution on Opportunity so that if the StageName of the Opportunity
is already set to “Closed Won” and now if any user is trying to Change the
Amount or Account of the Opportunity the user should get the Error.
Note: – You need to utilize the Apex Trigger to achieve this functionality
APEX TRIGGER 6: (Using Handler, Helper and Dispatcher Class)
Reference : [Link]
Create a Custom Field on the Account Object “Number of Contacts” with a
Number Data Type that does not have any decimal point in it.
Requirement (Roll-up Summary Trigger)
Develop a Solution that will count the related contact related to the
Account and store the information in the “Number of Contacts” field.
Note:- The contact can be created/deleted/undeleted and updated from
the Account Record. So please keep that in mind