Monday, August 24, 2015

Recursive triggers handling salesforce



Helper class

----------------------------------------------------------------------------------------------------------------


public class recursioncontrol
{

public static boolean flag=True;

}


-----------------------------------------------------------------------------------------------------------------


Trigger

-----------------------------------------------------------------------------------------------------------------


trigger firsttrigggercontact on Contact (before insert, before update,before delete,after insert,after update,after delete) {



List<Contact> c1=trigger.new;

List<Contact> c2=trigger.old;

if(trigger.isbefore)
{
if(trigger.isinsert)
{
system.debug('%%%%%%%%%%%%am from  before insert%%%%%%%%%%%%%'+c1);
}


if(trigger.isUpdate)
{

system.debug('%%%%%%%%%%%%am from  before update%%%%%%%%%%%%%'+c1);
system.debug('%%%%%%%%%%%%am from  before update%%%%%%Old Values%%%%%%%'+c2);
}


if(trigger.isDelete)
{
system.debug('%%%%%%%%%%%%am from before delete%%%%%%%%%%%%%'+c1);

system.debug('%%%%%%%%%%%%am from  before update%%%%%%%%%%%%%'+c1);
system.debug('%%%%%%%%%%%%am from  before update%%%%%%Old Values%%%%%%%'+c2);
}

}





if(trigger.isafter)
{
if(trigger.isinsert)
{

List<contact> c4=new List<contact>();
contact c5=new contact();
for(contact c3:trigger.new)
{
if(recursioncontrol.flag){
c5.LastName=c3.LastName;
c4.add(c5);
}
recursioncontrol.flag=false;
}
insert c4;

system.debug('%%%%%%%%%%%%am from  after insert%%%%%%%%%%%%%');
}


if(trigger.isUpdate)
{
system.debug('%%%%%%%%%%%%am from  after insert%%%%%%%%%%%%%');

}


if(trigger.isDelete)
{
system.debug('%%%%%%%%%%%%am from  after insert%%%%%%%%%%%%%');

}

}


}

No comments:

Post a Comment