You need to sign in to do that
Don't have an account?
saasdev
Using an update in an update Trigger - how to avoid the infinite recursive loop?
Hi
I am trying to create a Trigger that runs when an object is updated, and then updates another record of the same object type. This leads to an infinite recursive loop, as the Trigger then runs on that update, and so on....
I've tried changing the trigger so when it first updates, it changes a criteria field, so second time round it should not execute the update line, but this doesn't appear to work.
I think there might be a way around it using classes, but don't have enough Apex knowledge to even take a stab at what that might be.
Any help much appreciated.
Thanks
I am trying to create a Trigger that runs when an object is updated, and then updates another record of the same object type. This leads to an infinite recursive loop, as the Trigger then runs on that update, and so on....
I've tried changing the trigger so when it first updates, it changes a criteria field, so second time round it should not execute the update line, but this doesn't appear to work.
I think there might be a way around it using classes, but don't have enough Apex knowledge to even take a stab at what that might be.
Any help much appreciated.
Thanks
Hi,
Set a base condition for your recursion, to avoid the infinite recursion. you can set the base condition for your object by adding a boolean field in your object. and whenever you update a record of that object set the flag to true. and next time before updating see if it is false.
and to avoid simultaneous recursion do this:
you got to do a trick here. make a class that'll control the execution of your trigger.
ike this:
and in your code to avoid the simultaneous execution use th ProcessorControl.inFutureContext;
like this:
I have written such a trigger once.I make the trigger run with recursion update but it stoip somewhere,here is the code ,maybe it can help:
trigger ConcurrenceBeforeUpdate on Concurrence__c (before update) {
System.debug('## ConcurrenceBeforeUpdate CBU01 starts ');
boolean alreadySet = false;
integer pos = 0;
list<Concurrence__c> MainList = new list<Concurrence__c>();
Set<id> s = new Set<id>();
//populating the set of accounts
for(Concurrence__c c1: [
Select c.Principal__c, c.Opportunite__c, c.Compte__c
From Concurrence__c c
where c.Opportunite__c =:trigger.new[0].Opportunite__c
]){
s.add(c1.compte__c);
}
System.debug('## Retrieving existing main concurrence ');
MainList = [
Select c.Principal__c, c.Opportunite__c, c.Compte__c
From Concurrence__c c
where (c.Principal__c = true and c.Opportunite__c =:trigger.new[0].Opportunite__c)
];
System.debug('## Looping through list to set only one main concurrence ');
// CONCURRENCE
for (Integer i=0;i<trigger.new.size();i++){
System.debug('## Checking for main concurrence');
if( trigger.new[i].Principal__c == true && (!alreadySet))
{
if(trigger.new[i].Compte__c != null){
alreadySet = true;
pos = i;
}
else
trigger.new[i].Principal__c = false;
}
else if( trigger.new[i].Principal__c == true && (alreadySet))
{
if(trigger.new[i].Compte__c != null){
trigger.new[pos].Principal__c = false;
pos = i;
}
else
trigger.new[i].Principal__c = false;
}
System.debug('## Checking for duplicate accounts');
if(trigger.new[i].Compte__c == null)
{
trigger.new[i].addError('Le Compte ne doit pas être vide');
}
if(s.contains(trigger.new[i].Compte__c) && (trigger.new[i].Compte__c != trigger.old[i].Compte__c))
{
trigger.new[i].adderror('Ce Compte existe déjà');
}
}
//check whether there is already a main concurrence and a new main
if(!MainList.isEmpty() && alreadySet){
System.debug('## running recursive update <<<<<<< ');
Mainlist[0].Principal__c = false;
update MainList;
}
System.debug('## position of updated concurrence <<<<<<< ' + pos);
System.debug('## ConcurrenceBeforeUpdate CBU01 ends ');
}
This trigger work for the custom object Concurrence__c which has the same functionality as our standard partner on opportunity.