function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
akkkakkk 

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 .

Thanks
akkk
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.  

Thanks.
akkkakkk
Hi ANUTEJ

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

Thanks
 
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.
akkkakkk
Hi ANUTEJ,

YES, correct this is Scenario.


Thanks
Akk
ANUTEJANUTEJ (Salesforce Developers) 
Can you check if this works:
 
Trigger ContactTrigger on Contact(after update){
    List<ID> accIDList = new List<ID>();
    if(Trigger.isafter||Trigger.isUpdate){
        for(Contact con:trigger.new){
            accIDList.add(con.AccountID);
        }
        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(String.isNotBlank(c.AccountId)){
        	if(!ConMap.containsKey(con.AccountId)) {
            	ConMap.put(c.AccountId, new List<Contact>());
        	}
        	ConMap.get(c.AccountId).add(c);
    	}
        }

        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.id=Cin.id;
    		c.Total__c= CountTotal;
    		toupdate.add(c);
    		}
		}

		if(toupdate.size()>0)
		{update toupdate;}
    }}

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

Thanks.