0% found this document useful (0 votes)
375 views3 pages

Apex Trigger - Salesforce

Triggers in Salesforce are Apex code that execute when data manipulation language (DML) operations occur on objects. There are two types of triggers: before triggers which execute prior to DML operations, and after triggers which execute after. Triggers can access context variables like Trigger.new which provide information about the records being inserted, updated, etc. To define a trigger, use the trigger keyword followed by the object name and trigger events to specify when it will fire.

Uploaded by

Niveditha Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
375 views3 pages

Apex Trigger - Salesforce

Triggers in Salesforce are Apex code that execute when data manipulation language (DML) operations occur on objects. There are two types of triggers: before triggers which execute prior to DML operations, and after triggers which execute after. Triggers can access context variables like Trigger.new which provide information about the records being inserted, updated, etc. To define a trigger, use the trigger keyword followed by the object name and trigger events to specify when it will fire.

Uploaded by

Niveditha Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Apex Trigger:

Triggers in Salesforce are programmatic event handlers which is an Apex Code that gets
executed when a DML operation occurs on an sObject record.
Apex Triggers runs on Server side, not on client side.
Triggers can handle Data Manipulation language (DML) operations, can execute SOQL and can
call custom Apex methods.
Different Types of Trigger in Salesforce:
There are two types of Triggers in Salesforce:
1. Before Triggers.
2. After Triggers.

Events in Trigger:

✓ Before Insert
✓ After Insert
✓ Before Update
✓ After Update
✓ Before Delete
✓ After Delete
✓ After Undelete

To define a trigger, use the following syntax:


trigger trigger_name on ObjectName (trigger_events){
//code block
}
i.e
trigger MyTrigger on Account (before Insert, After Insert){
//code block
}

Apex Trigger always start with a keyword trigger


We can declare more than one trigger event in one trigger, but each should be separated by
comma. The events we can specify in an Apex Trigger are as mentioned above in “Events in
Trigger” section.

Page 1 of 3
Context Variable in Trigger:
There are 12 System defined class which contains 12 implicit i.e context variable which can
access at run time. All Trigger Context variables are prefixed with “Trigger”
Ex: Trigger.new
Below are the Context Variable:
1. Trigger.new: It returns the new version of the object records.
2. Trigger.new: It returns the old version of the object records.
3. isInsert: It returns true, if the code inside trigger context is executed due to an insert
operation.
4. isUpdate: It returns true, if the code inside trigger context is executed due to an update
operation.
5. isDelete: It returns true, if the code inside trigger context is executed due to delete
operation.
6. isUndelete: It returns true, if the code inside trigger context is executed due to undelete
operation. i.e when we restore date from recycle bin
7. isBefore: It returns true, if the code inside the trigger context is executed before the
record is saved.
8. isAfter: It returns true, if the code inside the trigger context is executed before the record
is saved.
9. isExecuting: It returns true, if the current apex code is trigger.
10. newMap: It returns a map of new version of sObjects which contains an ID’s as a key and
the old versions of the sObject records as value. This map is only available in before
update, after insert, and after update triggers.
11. oldMap: It returns a map of old version of sObjects which contains an ID’s as a key and
the new versions of the sObject records as value. This map is only available for only
update and delete trigger.
12. size: It returns the size of the manipulated record. It will return only if you will insert one
record, It will return the size of the record you are inserting, updating, or deleting or
undeleting.

Page 2 of 3
Page 3 of 3

You might also like