A Salesforce trigger is Apex code that executes when we do some DML operation in salesforce object.
In order to execution of trigger it is broadly of two types ,After Trigger and Before Trigger.
Basic syntax of trigger:-
trigger triggerName on ObjectName (trigger_events) {
//code_block
}
trigger TestTrigger on Case (Before Insert,After Insert,Before Update ,After Update ,Before Delete ,After Delete,After Undelete) {
//Code Block
}
[Note:-Keep in mind that only the trigger of that particular object will invoke in which object we will do DML operation, not like you will insert a record in child object and the trigger in parent object will invoke . It is possible to do some some data manipulation in different object but any way the trigger invoke only the object in which we will do Dml operation].
We can declare more than one trigger event in one trigger ,but each should be separated by comma.
Below are the possible event for trigger.
>>Before Insert
>>Before Update
>> Before Delete
>>After Insert
>>After Update
>>After Delete
>> After Undelete
There is a System defind class called Trigger which contains 12(twelve) implicit variable,which we can access at run time.
Below are the varibales with description.
1.isExecuting-It rutuns ture if the any code inside trigger context is executing.That means you can test whether your trigger is excuting or not by the help of this variable.
2.isBefore-It rutuns ture if the any code inside trigger context is executing before record is saved to the database.
3.isAfter-It rutuns ture if the any code inside trigger context is executing after record is saved to the database.
4.isInsert-It rutuns ture if the any code inside trigger context is executing due to an insert operation.
5.isUpdate-It rutuns ture if the any code inside trigger context is executing due to an update operation.
6.isDelete-It rutuns ture if the any code inside trigger context is executing due to a delete operation.
7.isUnDelete-It rutuns ture if the any code inside trigger context is executing due to undelete operation i,e when we recovered data from recyclebin .
8.new-It returns the new version of the object record.
9.newMap-It returns a map which contains an IDs 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.
10.old-It returns the old version of the object record.
11.oldMap-It returns a map which contains an IDs as a key and the new versions of the sObject records as value.This map is available for only update and delete trigger.
12.size- It return the size of the trigger.It will return one if you will insert one record, It will return the size of the record you are inserting ,updating or deleting or undeleting.
Examples to test the implicit variable:
trigger TestTrigger on Case (Before Insert,After Insert,Before Update ,After Update ,Before Delete ,After Delete,After Undelete) {
System.debug('*********testExecution*************'+Trigger.isExecuting);
System.debug('****************size of trigger***************'+Trigger.size);
if(Trigger.isBefore){
if(Trigger.isInsert){
for(Case cs:Trigger.new){
System.debug('********beforeInsertNew************'+Trigger.new);
}
}
if(Trigger.isUpdate){
for(Case cs:Trigger.new){
System.debug('********beforeUpdateNew************'+Trigger.new);
System.debug('**********beforeUpdateOld**********'+Trigger.old);
System.debug('********beforeMapUpdateNewMap************'+Trigger.newMap);
System.debug('**********beforeMapUpdateOldMap**********'+Trigger.oldMap);
}
}
if(Trigger.isDelete){
for(Case cs:Trigger.old){
System.debug('**********beforeDeleteOld**********'+Trigger.old);
System.debug('**********beforeMapDeleteOldMap**********'+Trigger.oldMap);
}
}
}
if(Trigger.isAfter){
if(Trigger.isInsert){
for(Case cs:Trigger.new){
System.debug('**********afterInsertNew**********'+Trigger.new);
System.debug('**********afterInsertNewMap**********'+Trigger.newMap);
}
}
if(Trigger.isUpdate){
for(Case cs:Trigger.new){
System.debug('**********afterUpdateNew**********'+Trigger.new);
System.debug('*************afterUpdateOld*******'+Trigger.Old);
System.debug('**********afterUpdateNewMap**********'+Trigger.newMap);
System.debug('*************afterUpdateOldMap*******'+Trigger.oldMap);
}
}
if(Trigger.isDelete){
for(Case cs:Trigger.old){
System.debug('*************afterDeleteold*******'+Trigger.Old);
System.debug('*************afterDeleteoldMap*******'+Trigger.oldMap);
}
}
if(Trigger.isUnDelete){
for(Case cs:Trigger.new){
System.debug('**********afterunUndeleteNew**********'+Trigger.new);
System.debug('**********afterunUndeleteNewMap**********'+Trigger.newMap);
}
}
}
}
[Note: Just copy this code and paste in case object or else in which object you want to test just the object name in the above example.After saving the trigger you can insert ,update ,delete and undelete records and just create debug log for different situation and observe.]
Below is the steps how to add this trigger in sales force in case object.
After that follow the below steps to create the debug log.
click Your Name ➤ Setup ➤ Monitoring➤ Debug logs
Click on New button to set the debug log in your name .
Now you can create record for case object and try to observe the debug log and just realize all context variable.
Just Insert ,update delete and undelete one record from recycle bin.
In order to execution of trigger it is broadly of two types ,After Trigger and Before Trigger.
Basic syntax of trigger:-
trigger triggerName on ObjectName (trigger_events) {
//code_block
}
trigger TestTrigger on Case (Before Insert,After Insert,Before Update ,After Update ,Before Delete ,After Delete,After Undelete) {
//Code Block
}
[Note:-Keep in mind that only the trigger of that particular object will invoke in which object we will do DML operation, not like you will insert a record in child object and the trigger in parent object will invoke . It is possible to do some some data manipulation in different object but any way the trigger invoke only the object in which we will do Dml operation].
We can declare more than one trigger event in one trigger ,but each should be separated by comma.
Below are the possible event for trigger.
>>Before Insert
>>Before Update
>> Before Delete
>>After Insert
>>After Update
>>After Delete
>> After Undelete
There is a System defind class called Trigger which contains 12(twelve) implicit variable,which we can access at run time.
Below are the varibales with description.
1.isExecuting-It rutuns ture if the any code inside trigger context is executing.That means you can test whether your trigger is excuting or not by the help of this variable.
2.isBefore-It rutuns ture if the any code inside trigger context is executing before record is saved to the database.
3.isAfter-It rutuns ture if the any code inside trigger context is executing after record is saved to the database.
4.isInsert-It rutuns ture if the any code inside trigger context is executing due to an insert operation.
5.isUpdate-It rutuns ture if the any code inside trigger context is executing due to an update operation.
6.isDelete-It rutuns ture if the any code inside trigger context is executing due to a delete operation.
7.isUnDelete-It rutuns ture if the any code inside trigger context is executing due to undelete operation i,e when we recovered data from recyclebin .
8.new-It returns the new version of the object record.
9.newMap-It returns a map which contains an IDs 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.
10.old-It returns the old version of the object record.
11.oldMap-It returns a map which contains an IDs as a key and the new versions of the sObject records as value.This map is available for only update and delete trigger.
12.size- It return the size of the trigger.It will return one if you will insert one record, It will return the size of the record you are inserting ,updating or deleting or undeleting.
Examples to test the implicit variable:
trigger TestTrigger on Case (Before Insert,After Insert,Before Update ,After Update ,Before Delete ,After Delete,After Undelete) {
System.debug('*********testExecution*************'+Trigger.isExecuting);
System.debug('****************size of trigger***************'+Trigger.size);
if(Trigger.isBefore){
if(Trigger.isInsert){
for(Case cs:Trigger.new){
System.debug('********beforeInsertNew************'+Trigger.new);
}
}
if(Trigger.isUpdate){
for(Case cs:Trigger.new){
System.debug('********beforeUpdateNew************'+Trigger.new);
System.debug('**********beforeUpdateOld**********'+Trigger.old);
System.debug('********beforeMapUpdateNewMap************'+Trigger.newMap);
System.debug('**********beforeMapUpdateOldMap**********'+Trigger.oldMap);
}
}
if(Trigger.isDelete){
for(Case cs:Trigger.old){
System.debug('**********beforeDeleteOld**********'+Trigger.old);
System.debug('**********beforeMapDeleteOldMap**********'+Trigger.oldMap);
}
}
}
if(Trigger.isAfter){
if(Trigger.isInsert){
for(Case cs:Trigger.new){
System.debug('**********afterInsertNew**********'+Trigger.new);
System.debug('**********afterInsertNewMap**********'+Trigger.newMap);
}
}
if(Trigger.isUpdate){
for(Case cs:Trigger.new){
System.debug('**********afterUpdateNew**********'+Trigger.new);
System.debug('*************afterUpdateOld*******'+Trigger.Old);
System.debug('**********afterUpdateNewMap**********'+Trigger.newMap);
System.debug('*************afterUpdateOldMap*******'+Trigger.oldMap);
}
}
if(Trigger.isDelete){
for(Case cs:Trigger.old){
System.debug('*************afterDeleteold*******'+Trigger.Old);
System.debug('*************afterDeleteoldMap*******'+Trigger.oldMap);
}
}
if(Trigger.isUnDelete){
for(Case cs:Trigger.new){
System.debug('**********afterunUndeleteNew**********'+Trigger.new);
System.debug('**********afterunUndeleteNewMap**********'+Trigger.newMap);
}
}
}
}
[Note: Just copy this code and paste in case object or else in which object you want to test just the object name in the above example.After saving the trigger you can insert ,update ,delete and undelete records and just create debug log for different situation and observe.]
Below is the steps how to add this trigger in sales force in case object.
click Your Name ➤ Setup ➤ Cases ➤ Case ➤ Triggers ➤ New
Just remove the existing code and paste the above code ans save the code .
After that follow the below steps to create the debug log.
click Your Name ➤ Setup ➤ Monitoring➤ Debug logs
Click on New button to set the debug log in your name .
Now you can create record for case object and try to observe the debug log and just realize all context variable.
Just Insert ,update delete and undelete one record from recycle bin.
No comments:
Post a Comment