You need to sign in to do that
Don't have an account?
sfdc_newbie17
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
DoctorFee : 10000
HospitalFee
TotalFee
Here Question is HospitalFee Should be 20000 by default
Totalfee = DoctorFee+ HospitalFee
How can we write program by using trigger in salesforce
Note : If I enter Doctorfee:10000 then
HospitalFee :20000
TotalFee:30000
Please let me know friends
trigger total_amount2 on Contact (after insert, after update, after delete, after undelete) {
Set<ID> setID = new Set<ID>();
List<Account> updAcc = new List<Account>();
if(trigger.isinsert || trigger.isundelete){
List<Contact> oldContatcs = Trigger.New;
for(Contact c: oldContatcs){
setID.add(c.AccountId);
}
}
else if(trigger.isDelete){
for(Contact c : trigger.old){
setID.add(c.AccountId);
}
}
else if(trigger.isUpdate){
for(Contact c : trigger.new){
if(c.AccountId != null){
if(trigger.oldmap.get(c.id).AccountId != c.AccountId){
setID.add(c.AccountId);
}
}
setID.add(trigger.oldmap.get(c.id).AccountId);
}
}
if(setid.size() > 0){
updAcc = [select id,name,Amount__c,(select id,name,Amount__c from Contacts) from Account where id IN : setID];
}
for(Account acc : updAcc){
Decimal val = 0;
for(Contact con : acc.Contacts){
if(con.Amount__c != null){
val += con.Amount__c;
}
}
acc.Amount__c = val;
}
update updAcc;
}