You need to sign in to do that
Don't have an account?

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';
}
}
}
}
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';
}
}
}
}
Use Boolean static variable to prevent recursion : https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US
All Answers
Use Boolean static variable to prevent recursion : https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US
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.
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;
}
}
}
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