Here i wrote two triggers on account and
contact with names called continsert on
account and accountupdate on contact
in continsert trigger
i am inserting contact with name of Account
at the time of update
in accountupdate
trigger i am updating account at the
time of contact insertion
See the below
triggers:
-------------------------------------
1)Trigger on Account
to insert a contact when account is update
trigger continsert on
Account (after update) {
List<contact> lstcontact=new
List<Contact>();
for(Account acc:Trigger.new){
Contact cnt=new Contact();
cnt.lastname=acc.name;
cnt.accountid=acc.id;
lstcontact.add(cnt);
}
if(lstcontact!=null &&
lstcontact.size()>0){
insert lstcontact;
}
}
2)Trigger on Contact
to update the account when contact is inserted
trigger accountupdate
on Contact (after insert) {
List<Account> lstaccount=new
List<Account>();
for(Contact c:Trigger.new){
if(c.accountid!=null){
Account acc=new
Account(id=c.accountid);
acc.description='contact is
created with name:'+c.lastname;
lstaccount.add(acc);
}
}
if(lstaccount!=null &&
lstaccount.size()>0){
update lstaccount;
}
}
Now when i am trying to insert contact then account will update then continsert on account is also fired with
accountupdate on contact because here in accountupdate trigger we are updating
account.
When i am trying to
update an account then contact is inserting then accountupdate on contact is
alsos fired with continsert trigger on account because here in continsert
trigger we are inserting contact.
So it throws an error
and it is because of recursive calling.
To Avoid or Remove
the recursive calling from above
triggers please follow the
following instructions
=========================================================================
So to avoid recurive
calling we need to write a class with static boolean variable and use it at the
DML statements on both triggers.
see the below code
for recursiveavoid class with boolean variable and modified triggers by using
boolean variable at the DML statements.
Please use the same
as i am explaining in your recurive
trigger Message update and Autotaskupdate some thing
class with boolean
variable:
===================
public class
recursiveavoid{
public static boolean recursive=false;
}
See the below
triggers:
-------------------------------------
1)Trigger on Account
to insert a contact when account is update
trigger continsert on
Account (after update) {
List<contact> lstcontact=new
List<Contact>();
for(Account acc:Trigger.new){
Contact cnt=new Contact();
cnt.lastname=acc.name;
cnt.accountid=acc.id;
lstcontact.add(cnt);
}
//Here i am using
boolean variable called
"recursive" to avoid reursive calling from trigger to another trigger
if(lstcontact!=null &&
lstcontact.size()>0 && recursiveavoid.recursive==false){
recursiveavoid.recursive=true;
insert lstcontact;
}
}
2)Trigger on Contact
to update the account when contact is inserted
trigger accountupdate
on Contact (after insert) {
List<Account> lstaccount=new
List<Account>();
for(Contact c:Trigger.new){
if(c.accountid!=null){
Account acc=new
Account(id=c.accountid);
acc.description='contact is
created with name:'+c.lastname;
lstaccount.add(acc);
}
}
//Here i am using
boolean variable called
"recursive" to avoid reursive calling from trigger to another trigger
if(lstaccount!=null &&
lstaccount.size()>0 && recursiveavoid.recursive==false){
recursiveavoid.recursive=true;
update lstaccount;
}
}
No comments:
Post a Comment