You need to sign in to do that
Don't have an account?
jai.s
Trigger to update sum of Child records into Parent object Custome Field
Hi,
I have scenario that update a custom field(Amount__C) of parent(Account) with sum of all child records (Total_Price) from Child Object(Invoice Line Item).
I have to wirte a trigger for above scenario, if any one have sample code please share it.
Thanks in advance,
Shaik
I have scenario that update a custom field(Amount__C) of parent(Account) with sum of all child records (Total_Price) from Child Object(Invoice Line Item).
I have to wirte a trigger for above scenario, if any one have sample code please share it.
Thanks in advance,
Shaik
A sample trigger code which fires on child(Opportunity) and updates parent(Account). Ignore typo's if any.
trigger UpdateNoOfOpp on Opportunity (after insert,after update,after delete,after undelete) {
List<Account> list_Account= new List<Account>();
set<Id> set_Opportunity = new set<Id>();
for(Opportunity objOpp: trigger.new){
set_Opportunity.add(objOpp.AccountId);
}
Decimal Sum;
for(Account objAccount : [SELECT Id,Name,(SELECT Id,Name,Price FROM Opportunities) FROM Account WHERE Id IN: set_Opportunity]){
Sum=0;
for(Opportunity objOpp01: objAccount.Opportunities ){
Sum+=objOpp01.Price ;
}
objAccount.Amount__c=Sum;
list_Account.add(objAccount);
}
update list_Account;
}
As there is master detail relationship between account and opportunity then roll up summary option should also work.
If an account is created and ParentId is populated, update Parent Account's AnnualRevenue with Sum of AnnualRevenue of child accounts.
Ex: Account "Parent1" has 2 child account's "child1" and "child2" with annualrevenue of 10000,20000 each. Account "Parent1" annualrevenue should be 30000.
If anyone have code, please share it,
Thanks in advance
vinod