+ Start a Discussion

count the sum of child records on the child object not on the parent object with trigger ?

Hi all
is it possbile ?
count the sum of the child records on the child object not on the Parent object with trigger .

ANUTEJANUTEJ (Salesforce Developers) 
Hi Ak,

>> https://developer.salesforce.com/forums/?id=906F0000000D9dvIAC

In the above examples depending on the relationship between the objects there are different ways to achieve the above use case, can you try checking them.

For quick reference, below is the best answer selected in above link:

Generally these kinds of requirements we can achieve in 2 ways:

--> If the relationship between the objects is Master-Details then we can use OTB functionality called the 'Roll-Up Summary' field.
--> If the relationship between the objects is Lookup then have to create multiple triggers to fulfill this functionality.
        Insert of Child record:
           --> Need to write an after insert trigger to update the Parent record with calculated sum value.
        Update of Child record:
           --> Make sure that the Parent information got changed.
           --> Need to write an after update trigger and get both old and new Parent information and perform the re-calculation of sum.
        Delete of Child record:
           --> Need to write an after delete trigger to update the Parent record with re-calculated sum value.
        Undelete of Child record:
           --> Need to write an after undelete trigger to update the Parent record with re-calculated sum value.

Also, look into the below options as well from other posts:

Option 1:- Process builder
1) https://developer.salesforce.com/forums/?id=906F0000000BHvJIAW
2) https://developer.salesforce.com/forums/?id=906F0000000B2C0IAK

Option 2:- Trigger
1) https://developer.salesforce.com/forums/?id=906F00000008yWuIAI
2) https://github.com/abhinavguptas/Salesforce-Lookup-Rollup-Summaries
3) http://www.anthonyvictorio.com/salesforce/roll-up-summary-utility/
4) http://www.iterativelogic.com/developing-custom-apex-rollup-summary-field-logic/
5) http://blog.jeffdouglas.com/2011/08/23/salesforce-trigger-when-rollups-summaries-not-possible/

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  


this is not currect answer of my question becasue want to sum of count of the child records update on child record not on the parent records

ANUTEJANUTEJ (Salesforce Developers) 
As per my understanding so if a parent record is A and it has child records b,c,d then the sum of lets say amount field on child records b,c,d should be visible on these records, if this is not the scenario can you correct it with correct explanation and an example to look into it further.

YES, correct this is Scenario.

ANUTEJANUTEJ (Salesforce Developers) 
Can you check if this works:
Trigger ContactTrigger on Contact(after update){
    List<ID> accIDList = new List<ID>();
        for(Contact con:trigger.new){
        List<Contact> conlist = new list<contact>;
        Map<id,list<Contact>> ConMap= new Map<id, list<Cotact>>();
        List<contact toupdate= new List<contact>();
        conlist= [select id,AccountId,Amount__c from Contact];
        for(contact c: conlist)
        	if(!ConMap.containsKey(con.AccountId)) {
            	ConMap.put(c.AccountId, new List<Contact>());

        for (Id key : ConMap.keySet()) {
    		List<Contact> tempCon = ConMap.get(key);
    		Integer CountTotal=0;
    		for(Contact Cin:tempCon)
    		CountTotal=CountTotal+ Cin.Amount__c;
    		for(Contact Cin: tempCon)
    		Contact c= new Contact();
    		c.Total__c= CountTotal;

		{update toupdate;}

Please note this is a sample snippet and you can modify the above snippet as per your need.