What is a Trigger?
Trigger can invoke Apex code, they come in handy when we want to perform some custom task just before or after a record is either created updated or deleted.
What are the various event on which a trigger can fire?
A trigger is a set of statement which can be executed on the following events:
- Undelete
- Update
- Merge
- Delete
- Upsert
- Insert
Broadly classify the Trigger?
Triggers can be broadly classified as before or after Trigger.
- Before triggers are used to perform a task before a record is inserted or updated or deleted.
- After triggers are used if we want to use the information set by Salesforce system and to make changes in the other records
What are the considerations while implementing the Triggers?
Consider the following before implementing the triggers.
- Upsert trigger fires on 4 different events :- before(insert, update), after (insert, update)
- Merge trigger are fired on both events on delete
- Field history is updated after the trigger has successfully finished processing data.
- Any callout should be asynchronous so that trigger does not have to wait for the response.
- A trigger cannot have a static keyword in its code.
- If a trigger completes successfully the changes are committed to the database and if it fails the transaction is rolled back.
Read the Apex Developer Guide for more detailed considerations.
Write the syntax of Apex Trigger?
Trigger TName On ObjName(name the events){
……. Apex code here ……..
}
What are context variables in regards to trigger?
Following are the context variable available in triggers. Please note variable’s availability varies according to the type of trigger events.
- isExecuting
- isInsert
- isUpdate
- isDelete
- isBefore
- isAfter
- isUndelete
- new
- newMap
- old (update and delete only)
- oldMap (update and delete only)
- size
How is Trigger.New Different from Trigger.newMap?
Trigger.New variable returns the list of sObject which has invoked the trigger and Trigger.NewMap returns the map of ID’s with the newly entered records. NewMap is only available in after insert, before and after the update and after undelete.
How is Trigger.new different from Trigger.old?
Trigger.New variable returns the list of sObject which has invoked the trigger and Trigger.old returns a list of the older versions of the records which have invoked the trigger. Trigger.Old is only available in update and delete events
Can a trigger call a batch class?
Yes, we can call a batch class in the trigger as we do in the normal apex code.
Can a trigger make a call to Apex callout method?
we can call a callout method in Apex Trigger but the only condition is that it has to be an asynchronous callout because the trigger flow cannot wait on the response received by the callout method.
Define Recursive Trigger and how to avoid it?
There is a possibility that the result of the trigger can end up calling the same trigger again and can run in a loop, this is known as a recursive trigger. To avoid this scenario we should create a static variable and check the value of this variable before we execute anything in the trigger.
What do you mean by the bulkifying trigger?
A trigger that can handle both single record and thousands of record. It is capable of handling mass action.
Is there any limit on number of triggers define on an object?
We can define as many triggers on an object as we want but it is recommended to have one trigger per object because the order of execution of different trigger is not guaranteed and any trigger can fire first.
Can you explain the order of execution in Triggers?
Following is the order of execution of events which Salesforce perform before a DML Event.
- The record is loaded from the database or is initialized in case of upset statement.
- New record’s field values are overwriting the old values, now depending on the origin of the request this flow varies: if the request is from a UI page then the following validations are performed by Salesforce:
- Any layout specific rules are checked
- All the required values are checked at layout and field level
- All the field formats are validated along with the maximum length of field values
If the request originates other than UI then Salesforce only checks for Validation of foreign keys.
- Now all the before triggers are executed at the database.
- Most of the validations are performed again to verify that all the required fields are holding some values and are not null, at this step user defined validations are also executed and the only validation which is not repeated in this step are the rules specific to the layout.
- After the success of the previous step, the record is reviewed for duplicate records, by running the duplicate rule. If a duplicate is found the flow is stopped and no further actions performed.
- In this step, record is saved to the database but it not committed yet.
- Now all the after Triggers are executed.
- In this step, assignment rules are executed.
- Now if there is any auto-response rule is present then they are executed.
- Next in the queues are the workflow, they are executed after the auto response.
- If the workflow was updating a field, then the fields updated in this step and the flow after this step varies if this was the case.
- If a field was updated then the before and after update triggers are fired once more and standard validation are also executed again. Custom validation escalation rule and duplicate rules are not required to run again.
- Once the execution has reached this stage, then process is fired if there are any declared on the object.
- Now the escalation rules are executed.
- Entitlement rules are executed if any.
- If there are any roll-up summary field, then they are calculated at this step and the parent object go through the save process.
- Now the sharing rules are executed.
- If we reach this stage, then that means no error has occurred and the data is ready to be committed to the database and is committed now.
- Now if there is any post-commit logic like email, then that is executed.
0 Comments