function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BillbayBillbay 

How to prevent recursive triggering in this example?

Hi, I tried reading up on static boolean variables and tried using them with not much success. Can somebody help how I can prevent recursive triggering in the below example? Many thanks!

First Trigger updates Invoice a dollar value equal to the sum of the dollar value from Permits which have a matching Invoice number. As below:

trigger InvoiceAmountUpdate on Permit__c (after update) {

for(Permit__c obj : trigger.new)
{
    List<Permit__c> permit = [select Total1__c from Permit__c p where p.Invoice_Num__c = :obj.Invoice_Num__c];

    decimal sum = 0;
    for (Permit__c p : permit) {sum = sum + p.Total1__c;}
    List<bb_Invoice__c> invoice = [select Invoice_Amount__c from bb_Invoice__c i where i.id = :obj.Invoice_Num__c];
    
for (bb_Invoice__c i : invoice) {
        i.Invoice_Amount__c = sum;
        update i;
    }
}
}

Second Trigger updates payment amount on Permits when the full invoice value is paid. However, with each update on Permit, the first trigger is called ending in a recursive pattern

trigger PaymentStatusUpdate on bb_Invoice__c (before update) {

for(bb_Invoice__c obj : trigger.new)
{
   if (obj.Paid_Amount__c>=obj.Invoice_Amount__c) {
    obj.Payment_Status__c = 'Paid';
   
List<Permit__c> permit = [select Total1__c,Paid__c from Permit__c p where p.Invoice_Num__c = :obj.id];

    for (Permit__c p : permit) {
        p.Paid__c = p.Total1__c;
        update p;
    }
    } 

    else if (obj.Paid_Amount__c>0) {
    if (obj.Paid_Amount__c<obj.Invoice_Amount__c) {
    obj.Payment_Status__c = 'Partially Paid';
    } 
    }
}
Best Answer chosen by Billbay
Thiyagarajan Selvaraj (SFDC Developer)Thiyagarajan Selvaraj (SFDC Developer)
Hi Billbay,

Use Boolean static variable to prevent recursion : https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US

All Answers

Thiyagarajan Selvaraj (SFDC Developer)Thiyagarajan Selvaraj (SFDC Developer)
Hi Billbay,

Use Boolean static variable to prevent recursion : https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US
This was selected as the best answer
Kiran kumar 193Kiran kumar 193
Hi Bill,

Create one global static class and there you have to maintain static boolean variables. use those bollean variables in your trigger by making true or false conditions.

Regards,
Kiran.
ra811.3921220580267847E12ra811.3921220580267847E12
Hi,
Class staticVaribles
{

public static boolean isRecurssive=false;
}



trigger PaymentStatusUpdate on bb_Invoice__c (before update) {
if(staticVaribles.isRecurssive ==false)
{
for(bb_Invoice__c obj : trigger.new)
{
   if (obj.Paid_Amount__c>=obj.Invoice_Amount__c) {
    obj.Payment_Status__c = 'Paid';
   
List<Permit__c> permit = [select Total1__c,Paid__c from Permit__c p where p.Invoice_Num__c = :obj.id];

    for (Permit__c p : permit) {
        p.Paid__c = p.Total1__c;
        update p;
    }
    } 

    else if (obj.Paid_Amount__c>0) {
    if (obj.Paid_Amount__c<obj.Invoice_Amount__c) {
    obj.Payment_Status__c = 'Partially Paid';
    } 
    }
}
}



trigger InvoiceAmountUpdate on Permit__c (after update) {

for(Permit__c obj : trigger.new)
{
    List<Permit__c> permit = [select Total1__c from Permit__c p where p.Invoice_Num__c = :obj.Invoice_Num__c];

    decimal sum = 0;
    for (Permit__c p : permit) {sum = sum + p.Total1__c;}
    List<bb_Invoice__c> invoice = [select Invoice_Amount__c from bb_Invoice__c i where i.id = :obj.Invoice_Num__c];
    
for (bb_Invoice__c i : invoice) {
        i.Invoice_Amount__c = sum;
        update i;
        staticVaribles.isRecurssive = true;
    }
}
}
BillbayBillbay
Hi,

Thank you all for yor responses. Used the appraoch from Kiran and managed to resolve. Thanks also for taking the time Ra to incoporate how the static boolean variable can be passed in my code.

Thanks,
Ravi