+ Start a Discussion
jai.sjai.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
kiranmutturukiranmutturu
this is how custom products information is updating on opportunity.. This is a method called from a class which is called from custom products trigger..... 
 
public static void rollUpAmounts(List<ProductService__c> productServices)
    {
        Map<Id, Opportunity> opportunityMap = new Map<Id, Opportunity>();
        
        for (ProductService__c ps : productServices)
        {
            Opportunity opp = new Opportunity(Id = ps.Opportunity__c);
            opp.BookingsValue__c = 0;
            opportunityMap.put(ps.Opportunity__c, opp);
        }
        
        for (ProductService__c ps : [select Opportunity__c, BookingsValue__c 
                                     from ProductService__c 
                                     where Opportunity__c in :opportunityMap.keySet() ])
        {
            Opportunity opp = opportunityMap.get(ps.Opportunity__c);

            if (ps.BookingsValue__c != null)
            {
                opp.BookingsValue__c += ps.BookingsValue__c;
            }
        }
        
       update opportunityMap.values();
       
        
        }

 
Mahmood ButtMahmood Butt
You could use Rollup Summary on amount__c custom field. If its a parent-child relationship, as you mentioned in your question.
Rahul_SalesforceRahul_Salesforce
Roll up summary field can be used if you are using master detail relationship . In case you have a look up relationship then something like below should work.
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.
 
vinod reddy 21vinod reddy 21
Hi,  i am new to salesforce. I have a requirement on trigger.
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