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
himanshu huske 7himanshu huske 7 

System.SObjectException in rollUp summary for sum


Hi developers,
Please fix th issue for thisUser-added imagetrigger TotalAmt_Child_paraent on Child__c (after insert,after update, after delete,after undelete) {
    set<id> idSet = new  set<id>();
    List<Child__c> cList = new List<Child__c>();
    List<Parent__c> pList = new List<Parent__c>();
    if(trigger.isinsert ||trigger.isupdate ||trigger.isundelete){
        for(Child__c c : trigger.new){
            if(c.ParentMD__c != null)
                idSet.add(c.ParentMD__c);
        }
    }
    if(trigger.isdelete){
        for(Child__c c : trigger.old){
            if(c.ParentMD__c != null)
                idSet.add(c.ParentMD__c);
        }
    }    
    if(idSet.size()>0){
    decimal amt = 0;
    cList = [Select id, ParentMD__c from Child__c where ParentMD__c in: idSet];
    Map<id,List<Child__c>> prMap = new Map<id,List<Child__c>>();
    for(Child__c c : cList){
        prMap.put(c.ParentMD__c, new List<Child__c>());
        prMap.get(c.ParentMD__c).add(c);
    }
    pList = [Select id,TotalAmt__c from Parent__c where id in: prMap.keySet()];
        for(Parent__c p : pList){
            for(Child__c ch : prMap.get(p.id)){
                if(ch.MyAmt__c != null)
                    amt +=  ch.MyAmt__c; 
            }
            p.TotalAmt__c = amt;
        }
        update pList;
    }
    }
Sunil RathoreSunil Rathore
Hi Himanshu,

Add the MyAmt__c in the query on the child object.

i.e. the new query is like:

cList = [Select id, ParentMD__c, MyAmt__c from Child__c where ParentMD__c in: idSet];

Let me know if it helps you.

Many Thanks,
Sunil Rathore
 
himanshu huske 7himanshu huske 7
hi sunil,
Thank you for help, now its working, but it is not performing sum,
i created 4 child rec, but it is updating only one, can u overlook a code again..
 
Dushyant SonwarDushyant Sonwar
If you are writing a rollup trigger, then you can take the help below url :
https://blog.jeffdouglas.com/2009/07/30/roll-up-summary-fields-with-lookup-relationships-part-1/
https://medium.com/@palanic/roll-up-summary-from-child-account-to-patent-account-d0d97114e883
 
Sunil RathoreSunil Rathore
Hi Himanshu,

Replace this code
for(Child__c c : cList){
        prMap.put(c.ParentMD__c, new List<Child__c>());
        prMap.get(c.ParentMD__c).add(c);
}

with the following code:
List<Child__c>newCList = new List<Child__c>()
for(Child__c c : cList){
	if(prMap.containsKey(c.ParentMD__c){
		newCList.add(c);
		prMap.put(c.ParentMD__c, newCList);
	}else{
		newCList.clear();
		prMap.put(c.ParentMD__c, newCList.add(c));
	}
}
Let me know if it helps you out.

Many Thanks,
Sunil Rathore
​​​​​​​