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
hari0853701703454959hari0853701703454959 

My requirement: Creat a trigger on invoice line item to update account balence as sum fo all associated invoices amount for the account.(Create a custom field on Account Called "Account Balance" as currency data type)

Hi friends,

 1. custom object called invoice with field as
     a.invioce number(auto number)
     b.invioce date( date)
     c.Account Name(lookup with Account)
     d.invioce Amount(Roll up Summary,sumof allitems Price field of invioce Line Items object)
2.custom object called invoice Line Items with field as
    a.Invioce( Master Detail with Invioce)
    b.Product(Lookup with product)
    c.Quantity(Number)
     d.Unit Price(Currency)will contain price of one product
    e.Total Price((Formula, Quantity*Unit Price

My requirement:
Creat a  trigger on invoice line item to update account balence as sum fo all associated invoices amount for the account.(Create a custom field on Account Called "Account Balance" as currency data type)

Trigger updateamount on invoicelineitem (after insert){
       
    List <Account> acclist = new List<Account>();
    List<Id> accIds = new List<id>();
    for(invoicelineitem it : trigger.new){
          accIds.add(it.invoice.accountId);
     }

    for(account acc: [select id, invoice amount from account where id IN:accIds]) {
           for((invoicelineitem it : trigger.new){
               if(it.invoice.accountid == acc.id){
                       acc.invoiceamount = it.totalprice;
                       acclist.add(acc);
               }
           }
    }

   // update the accounts
    update acclist;

}
what are the changes I have done in trigger anybody can suggest 
logontokartiklogontokartik
HI Hari,

Firstly, I am not sure why you need a trigger on Invoice Line Items, instead of on Invoice as when the line item is created, the Invoice also gets updated with the new amount (via rollup summary), So all you have to do is write a trigger on invoice to update the Invoice Amount.

Whatever you are doing in Invoice Line Item makes sense, but then you need to write update and delete actions as well on Line Item.

Please recheck and see if it helps.

 
hari0853701703454959hari0853701703454959
Hi karti, My requirement: Creat a trigger on invoice line item to update account balence as sum fo all associated invoices amount for the account.(Create a custom field on Account Called "Account Balance" as currency data type). I need trigger for above requirements.If u Know send me as possible as. Regards, hari Regards, *Hareesh*