You need to sign in to do that
Don't have an account?
Amita Tatar 15
Trigger to update amount
Hi all,
I have two objects Booking and Invoice having Master Detail relationship.
i want to update a field on invoice which is a difference calculation. I have written a trigger but it is giving me some exceptions.
I wll post my code. Please help to fix the issue
I have two objects Booking and Invoice having Master Detail relationship.
i want to update a field on invoice which is a difference calculation. I have written a trigger but it is giving me some exceptions.
I wll post my code. Please help to fix the issue
trigger updateAmount on Invoice__c (after update) { Set<Id> bookings = new Set<Id>(); List<Invoice__c> invoiceToUpdate = new List<Invoice__c>(); for(Invoice__c inv:Trigger.new) { if (inv.Booking__c != null) { bookings.add(inv.Booking__c); system.debug('----------***Bookings****-------:'+bookings); } } Invoice__c invoiceCancel = [SELECT Id,Status__c,Grand_Total__c FROM Invoice__c WHERE Booking__c IN:bookings AND Status__c='Cancelled' ORDER BY createdDate DESC LIMIT 1]; System.debug('---------------invoiceCancel -------------'+invoiceCancel ); //Decimal grandAmt1 = //System.debug('---------------grandAmt1-------------'+grandAmt1); Invoice__c invoiceNew = [SELECT Id,Status__c,Grand_Total__c,Amount_Difference__c FROM Invoice__c WHERE Booking__c IN:bookings AND Status__c='New']; //Decimal grandAmt2 = //System.debug('---------------grandAmt2 -------------'+grandAmt2 ); for(Invoice__c inv:trigger.new) { Decimal GrtTotal = invoiceNew.Grand_Total__c - invoiceCancel.Grand_Total__c; inv.Amount_Difference__c = GrtTotal; System.debug('---------------Amount_Difference__c-------------'+inv.Amount_Difference__c ); Invoice__c invv = new Invoice__c(); invoiceNew.Amount_Difference__c = inv.Amount_Difference__c; } update invoiceNew; update invoiceToUpdate; }
Can you please elaborate more on the exception you are facing and which line is causing this exception?
More details from you helps us in helping you faster.
This SOQL query will potentially return a List<Invoice__c> not Invoice__c object.
You either have to add LIMIT 1 or process the list by looping through.
And, you are updating multiple Invoice__c objects inside an AFTER UPDATE trigger on the same object. This will cause recursive trigger issue.
You have to address that too.
Actually on line no 15 the query is retreiving 0 rows.
I want to run that query after invoice is created on booking and then check further conditions.How i should do that?
Hi Amita,
As per your error on line no.15, It seems that, There is not invoice present in database which have meet these crietrion : - Status__c='Cancelled' and Booking__c IN:bookings.
Please let me know, if it exist..
Thanks
Rajat maheshwari
rajatzmaheshwari@gmail.com
On line no 11, it does display record in system.debug. on line no 15 it is not displaying record because invoice is not inserted at that time.
Thanks