Before and After insert Triggers in Salesforce
Insert events trigger in Salesforce
There are two insert events.
- Before Insert
- After Insert
Before insert in Triggers
Before insert:
These triggers will be fired when we are trying to insert a new records into a specified object.
Before insert:
These triggers will be fired when we are trying to insert a new records into a specified object.
- Operations which we have written in trigger will be implemented before new records are saved to the database.
- In before insert, trigger.new stores the list of new records which we are trying to insert.
Trigger.new in before insert:
We have an object with three records.
We have an object with three records.
CID | Name | Age | Phone |
111 | aaa | 23 | 1234 |
222 | bbb | 22 | 6262 |
333 | ccc | 24 | 6666 |
In this object if we are trying to insert new records, into customer object.
444 | dddd | 26 | 7989 |
555 | eee | 28 | 2341 |
Then the new records which are trying to insert are stored in trigger.new in before event. Which means?
List<customer__c> cus = trigger.new;
List<customer__c> cus = trigger.new;
444 | dddd | 26 | 7989 |
555 | eee | 28 | 2341 |
These records are stored into Trigger.new.
Note: The records Which are stored in the trigger.new we can directly perform changes in before insert.
Eg:
For(customer__c c:Trigger.new)
{
c.Age__c=30;
}
Note: Before insert event will occur before new records are inserted into the database. So we cannot retrieve the new records using DML operations in before trigger i.e., when we have customers table with following records.
Note: The records Which are stored in the trigger.new we can directly perform changes in before insert.
Eg:
For(customer__c c:Trigger.new)
{
c.Age__c=30;
}
Note: Before insert event will occur before new records are inserted into the database. So we cannot retrieve the new records using DML operations in before trigger i.e., when we have customers table with following records.
CID | Name | Age | Phone |
111 | aaa | 23 | 1234 |
222 | bbb | 22 | 6262 |
333 | ccc | 24 | 6666 |
When we are trying to perform insert two new records.
444 | dddd | 26 | 7989 |
555 | eee | 28 | 2341 |
If there is any before insert trigger and we have written any DML in it.
trigger example on customer__c(before insert)
{
trigger example on customer__c(before insert)
{
List<customer__c> my = [select cid__c, Name, Age__c, Phone__c from customer__c];
/*this query will only fetch three records
444 | dddd | 26 | 7989 |
555 | eee | 28 | 2341 |
as remaining two records are not yet inserted*/
}
After insert in Triggers:
After insert:
}
After insert in Triggers:
After insert:
- These triggers will be fired after new records are successfully saved to the database.
- We can use Trigger.new to refer to the list of new records which we have inserted.
- On trigger.new we can only perform read only operations.
- On the new list of records we can perform DML operations.
Note: On any records that are successfully saved to database. If we want to perform any changes on those records we have to perform DML operations
We have an object with three records.
We have an object with three records.
CID | Name | Age | Phone |
111 | aaa | 23 | 1234 |
222 | bbb | 22 | 6262 |
333 | ccc | 24 | 6666 |
When we insert new records
444 | dddd | 26 | 7989 |
555 | eee | 28 | 2341 |
After triggers will be performed after committing the new records into database which means
CID | Name | Age | Phone |
111 | aaa | 23 | 1234 |
222 | bbb | 22 | 6262 |
333 | ccc | 24 | 6666 |
444 | dddd | 26 | 7989 |
555 | eee | 28 | 2341 |
After saving this records then after insert trigger will be called, so operation written in trigger will be performed after records are successfully inserted.
Interview questions on triggers In salesforce
1.What is trigger ?
Ans: Trigger is piece of code that is executes before and after a record is Inserted/Updated/Deleted from the force.com database.
2.What are different types of triggers in sfdc?
Ans: 1.Before Triggers-These triggers are fired before the data is saved into the database.
2.After Triggers-These triggers are fired after the data is saved into the database.
3.What are trigger context variables?
Ans:
Trigger.isInsert: Returns true if the trigger was fired due to insert operation.
Trigger.isUpdate: Returns true if the trigger was fired due to update operation.
Trigger.isDelete: Returns true if the trigger was fired due to delete operation.
Trigger.isBefore: Returns true if the trigger was fired before record is saved.
Trigger.isAfter: Returns true if the trigger was fired after record is saved.
Trigger.New: Returns a list of new version of sObject records.
Trigger.Old: Returns a list of old version of sObject records.
Trigger.NewMap: Returns a map of new version of sObject records. (map is stored in the form of map)
Trigger.OldMap: Returns a map of old version of sObject records. (map is stored in the form of map)
Trigger.Size: Returns a integer (total number of records invoked due to trigger invocation for the both old and new)
Trigger.isExecuting: Returns true if the current apex code is a trigger.
4.What is the difference between Trigger.New and Trigger.NewMap?
Ans:
Trigger.New is Returns a list of new version of sObject records but Trigger.NewMap is Returns a map of new version of sObject records.
5.What is the difference between Trigger.New and Trigger.Old?
Ans:
Trigger.New is Returns a list of new version of sObject records and Trigger.Old is Returns a list of old version of sObject records.
6.What is the difference between Trigger.New and Trigger.Old in update triggers?
Ans:
7.Can we call batch apex from the Trigger?
Ans: A batch apex can be called from a class as well as from trigger code.
In your Trigger code something like below :-
// BatchClass is the name of batchclass
BatchClass bh = new BatchClass();
Database.executeBacth(bh);
8.What are the problems you have encountered when calling batch apex from the trigger?
Ans:
9.Can we call the callouts from trigger?
Ans: yes we can. It is same as usual class method calling from trigger. The only difference being the method should always be asynchronous with @future
10.What are the problems you have encountered when calling apex the callouts in trigger?
Ans:
11.What is the recursive trigger?
Ans: Recursion occurs in trigger if your trigger has a same DML statement and the same dml condition is used in trigger firing condition on the same object(on which trigger has been written)
12.What is the bulkifying triggers?
Ans: By default every trigger is a bulk trigger which is used to process the multiple records at a time as a batch. For each batch of 200 records.
13.What is the use of future methods in triggers?
Ans: Using @Future annotation we can convert the Trigger into a Asynchrinous Class and we can use a Callout method.
14.What is the order of executing the trigger apex?
Ans:
1. Executes all before triggers.
2. Validation rules.
3. Executes all after triggers.
4. Executes assignment rules.
5. Executes auto-response rules.
6. Executes workflow rules.
7. If there are workflow field updates, updates the record again.
8. If the record was updated with workflow field updates, fires before and after triggers one more time. Custom validation rules are not run again.
9. Executes escalation rules.
10. If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through save procedure.
11. If the parent record is updated, and a grand-parent record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Grand-parent record goes through save procedure.
12. Executes Criteria Based Sharing evaluation.
13. Commits all DML operations to the database.
14. Executes post-commit logic. Ex: Sending email.
15.How do we avoid recursive triggers?
Ans: Use a static variable in an Apex class to avoid an infinite loop. Static variables are local to the context of a Web request.
16.How many triggers we can define on a object?
Ans: We can write more than one trigger But it is not recommended .Best practice is One trigger On One object.
Ans: Trigger is piece of code that is executes before and after a record is Inserted/Updated/Deleted from the force.com database.
2.What are different types of triggers in sfdc?
Ans: 1.Before Triggers-These triggers are fired before the data is saved into the database.
2.After Triggers-These triggers are fired after the data is saved into the database.
3.What are trigger context variables?
Ans:
Trigger.isInsert: Returns true if the trigger was fired due to insert operation.
Trigger.isUpdate: Returns true if the trigger was fired due to update operation.
Trigger.isDelete: Returns true if the trigger was fired due to delete operation.
Trigger.isBefore: Returns true if the trigger was fired before record is saved.
Trigger.isAfter: Returns true if the trigger was fired after record is saved.
Trigger.New: Returns a list of new version of sObject records.
Trigger.Old: Returns a list of old version of sObject records.
Trigger.NewMap: Returns a map of new version of sObject records. (map is stored in the form of map)
Trigger.OldMap: Returns a map of old version of sObject records. (map is stored in the form of map)
Trigger.Size: Returns a integer (total number of records invoked due to trigger invocation for the both old and new)
Trigger.isExecuting: Returns true if the current apex code is a trigger.
4.What is the difference between Trigger.New and Trigger.NewMap?
Ans:
Trigger.New is Returns a list of new version of sObject records but Trigger.NewMap is Returns a map of new version of sObject records.
5.What is the difference between Trigger.New and Trigger.Old?
Ans:
Trigger.New is Returns a list of new version of sObject records and Trigger.Old is Returns a list of old version of sObject records.
6.What is the difference between Trigger.New and Trigger.Old in update triggers?
Ans:
7.Can we call batch apex from the Trigger?
Ans: A batch apex can be called from a class as well as from trigger code.
In your Trigger code something like below :-
// BatchClass is the name of batchclass
BatchClass bh = new BatchClass();
Database.executeBacth(bh);
8.What are the problems you have encountered when calling batch apex from the trigger?
Ans:
9.Can we call the callouts from trigger?
Ans: yes we can. It is same as usual class method calling from trigger. The only difference being the method should always be asynchronous with @future
10.What are the problems you have encountered when calling apex the callouts in trigger?
Ans:
11.What is the recursive trigger?
Ans: Recursion occurs in trigger if your trigger has a same DML statement and the same dml condition is used in trigger firing condition on the same object(on which trigger has been written)
12.What is the bulkifying triggers?
Ans: By default every trigger is a bulk trigger which is used to process the multiple records at a time as a batch. For each batch of 200 records.
13.What is the use of future methods in triggers?
Ans: Using @Future annotation we can convert the Trigger into a Asynchrinous Class and we can use a Callout method.
14.What is the order of executing the trigger apex?
Ans:
1. Executes all before triggers.
2. Validation rules.
3. Executes all after triggers.
4. Executes assignment rules.
5. Executes auto-response rules.
6. Executes workflow rules.
7. If there are workflow field updates, updates the record again.
8. If the record was updated with workflow field updates, fires before and after triggers one more time. Custom validation rules are not run again.
9. Executes escalation rules.
10. If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through save procedure.
11. If the parent record is updated, and a grand-parent record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Grand-parent record goes through save procedure.
12. Executes Criteria Based Sharing evaluation.
13. Commits all DML operations to the database.
14. Executes post-commit logic. Ex: Sending email.
15.How do we avoid recursive triggers?
Ans: Use a static variable in an Apex class to avoid an infinite loop. Static variables are local to the context of a Web request.
16.How many triggers we can define on a object?
Ans: We can write more than one trigger But it is not recommended .Best practice is One trigger On One object.
Thank you for watching my post please share my post your friends
0 Comments