i have 3 objects: invoice,invoice line item & standard Account.
relationships:
-
invoice & invoice line item—>master-detail.
-
Account & invoice—>lookup.
On the invoice object i have one Rollup summary field called total price(sum of all prices on invoice line item).
On account i have 1 field called Account_balance
On the save event of a invoice line item i want to update Account_balance field with rollup summary value of invoice object.
Is this possible with a trigger ?
Best Answer
This is definitely possible with a trigger. To give you some pseudo code, I would write your trigger on the
Invoice__c
object. It will handle any updates of theInvoice_Line_Item__c
object because of the rollup summary. You will want to have something like:You will have to clean that up and write your own tests, but that is the general idea. Every time an
Invoice_Line_Item__c
is updated and it changes the rollup summary field, this trigger will fire to update the account. There is some additional logic you may want to add, such as checking the old value of theTotal_Price__c
field with the new value and only running through Accounts who have hadInvoice__c
changes rather than all of them. There may be some other ways to tweak this as well, I just quickly wrote this up. Keep in mind this is not production ready by any means, but I hope this points you in the right direction.The approach above utilizes aggregate functions and setting Id fields for update.
EDIT: I originally had the following code, which should still work, but the above is more optimal.