Trigger is an Apex Code that executes before or after changes occurs to Salesforce records.
These changes includes operations like Insert, Update, Delete, Merge, Upsert and Undelete.
trigger TriggerName on ObjectName (trigger_events)
where trigger-events can be a comma-separated list of one or more of the following events:
Trigger firstTrigger on Account(before insert)
System.debug(‘I am before insert.’);
Trigger secondTrigger on Account(after insert)
System.debug(‘I am after insert.’);
Update Account a;
(Before update Trigger of Account a)
Inserts contact b;
(after insert of contact B)
Insert a; // over here we are updating Account a in its before trigger
// indirectly i.e. not allowed.
Note: If you update or delete a record in its before trigger, or delete a record in its after trigger you’ll receive an error and this includes both direct and indirect operations.
trigger ApexTrigger on Account (before update)
Contact c = new Contact(LastName = ‘Steve’);
trigger ApexTrigger on Contact (after insert)
Account a = [Select Name from Account Limit 1];
a.name = ‘Updated Account’;
MyException me = new MyException();
//update a; // Not Allowed
Triggers can modify other records of the same type as the records that initially fired the trigger.
As triggers can cause other records to change and because these changes can, in turn, fire more triggers, the apex runtime engine considers all such operations a single unit of work & sets limits on the number of operations that can be performed to prevent infinite recursion.
< < Previous
Next > >
The batch is going to start shortly. Fill the form given below to Register yourself now.
Fill the form below to get a demo of this course.
Online Salesforce Development Course is soon going to be launched. Please fill the form and we will notify you about the course.