+ Start a Discussion
Balaram AdminBalaram Admin 

how to write trigger update account when ever child object roll up value is updated?

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)
s_k_as_k_a
Here the steps:
Step 1. get the invoiceid from invoice line item. It comes from trigger.new.
Step 2. query the invoice to to get account id using the invoiceid from step1. Now you will have the acoountId
Step 3. Query the Account to get all invoices related to account like below.

For(Account acc : [Select id, Name, account_Balance__c,(select id, invoice_Ammount__c from invoices__r) from account where id in :accountIds])
{
   decimal amountTotal =0.00;  
  for(invoice__c in :acc.invoices__r)
    {
       amountTotal = amountTotal+in.invoice_Ammount__c;
    }
acc.account_Balance__c = amountTotal;
acctoUpdate.add(acc);
 }
//Update all accounts.
Balaram AdminBalaram Admin
Hi dudu.. thnks for the replying... how to get the id...i didn't understand ...please help me.... Step 1. get the invoiceid from invoice line item. It comes from trigger.new. Step 2. query the invoice to to get account id using the invoiceid from step1. Now you will have the acoountId i need code dudu please...
hari0853701703454959hari0853701703454959
I need the trigger the above reqiurements ...Balaram