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
Amita Tatar 15Amita 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 
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;                  
    
 }

 
LBKLBK
Hi Amita,

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.
LBKLBK
First glance at your trigger code says that you might have a problem in line 23.
 
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'];
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.
Amita Tatar 15Amita Tatar 15
Hey hi,

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?
Amita Tatar 15Amita Tatar 15
My requirement is that i want to calculate difference between two amoun fields on invoice one with status new and other with cancelled.
 
rajat Maheshwari 6rajat Maheshwari 6

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

 

Amita Tatar 15Amita Tatar 15
Hi Rajat,

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.
rajat Maheshwari 6rajat Maheshwari 6
Your trigger is working on update operation, so by this query It try to serach Invoice from database which meet criterion and unfortunately, as per the error, There is no any invoice present with crieterion in database.

Thanks