You need to sign in to do that
Don't have an account?

Trigger to calculate sum of value on child object
Hi all
I am trying to calcualte the sum of values on a custom object "invoice__c" to Opportunity which is a lookup relationship. As it's not MD I can't use rollup summary. The field on invoice is value and needs to be aggregrated the the "total invoice value" on the opportunty object.
Can anyone help, everything I've tried so far hasn't worked.
Thanks
I am trying to calcualte the sum of values on a custom object "invoice__c" to Opportunity which is a lookup relationship. As it's not MD I can't use rollup summary. The field on invoice is value and needs to be aggregrated the the "total invoice value" on the opportunty object.
Can anyone help, everything I've tried so far hasn't worked.
Thanks
Please try below code.
Let me know if you face any issue.
Thanks,
Parteek
All Answers
https://salesforce.stackexchange.com/questions/33650/how-to-write-a-rollup-summary-trigger-for-a-lookup-relationship
Please try below code.
Let me know if you face any issue.
Thanks,
Parteek
Thanks for this but getting the following error Variable does not exist: Opportunityid at line 7 column 25, also is the invoice__cs supposed to be invoice__c?
Please use the opportunity lookup field API which you have created in invoice object instead of Opportunityid . Secondly please use child relationship name of opportunity lookup field(Please add __r after that) instead of invoice__cs.
Thanks,
Parteek
Error: Compile Error:
(Select id,invoice_value__c from invoice_item__r) from Opportunity
^
ERROR at Row:1:Column:68
Didn't understand relationship 'invoice_item__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 27 column 18
Can you please share the screen shot of opportunity lookup field on invoice item object?
Thanks,
Parteek
I have also changed opportunity__r.id to opportunity__c
Thanks,
Parteek
Here you go. thanks again
Thanks,
Parteek
Only issue I'm facing is the 50k limit, is there anyway around this? I have approx 700k invoice records
<pre>
for ( AggregateResult result :
[ SELECT SUM(Value__c) total_value, Opportunity__c
FROM Invoice__c
WHERE Id IN :Trigger.new]
GROUP BY Opportunity__c
]
)
{
Trigger.newMap( (Id) result.get( "Opportunity__c" ) ).Total_Value__c = (Decimal) result.get( "total_value" );
}
</pre>
Please create a report of opportunity with invoice item and extract the opportunity id, invoice item id,value, total value fields. After that use pivot table in ms-excel and update the data from dataloader for now. after that trigger will automatically update the correct data on every edit of invoice item.
Let me know if you face any issue.
Thanks,
Parteek