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
Lakshmi SLakshmi S 

Trigger Issue : Updating parent object data from child object ?

Hi Team,

We are using trigger for updating count and sum from child to parent object. I am getting issue with one scenario.
Ex :
Parent -  A
Parent - B

Child -1.

Scenario : 1
------------------
Child -1 attached with Parent - A, now its updating Parent - A data.

Scenario : 2
---------------------
Child -1 attached with Parent- A, now i am updating the lookup field with Parent - B, Now Parent - B updating with child data but Parent - A data is not erased still it is showing Child - 1 data (sum and count).

Please let me know how can we resolve this issue.

Code :
----------
public static void rollupSumUpdate(Map<Id, child1__c> newRecs, Map<Id, child1__c> oldRecs){
   
        try{
            
            Set<String> parentIds = new Set<String>();
            
            for(Id chIds : newRecs.keySet()){
                
                child1__c tr = newRecs.get(chIds);
                
                if(tr.parentId__c != Null){
                    
                    parentIds.add(tr.parentId__c);
                    
                    System.debug('******If block -  Update Method - parentIds : **********'+parentIds);
                }
                else
                {
                    parentIds.add(oldRecs.get(chIds).parentId__c);
                    System.debug('******else block -  Update Method - parentIds : **********'+parentIds);
                }
                
            }
            
            Map<Id,parent__c> mapToUpdate = new Map<Id,parent__c>();
            
            if(parentIds.size() > 0){
                
                Map<Id,parent__c> fsOppMap = new Map<Id,parent__c>([Select Id,Amount__c,Trade_Count__c from parent__c where Id In :parentIds]);
                
                parent__c fsOpp = new parent__c();
                
                List<AggregateResult> ar = [Select parentId__c,Count(Id)tradeCount,Sum(Annual_Recurring_Revenue__c)arrSum,Sum(Non_Recurring_Revenue__c)nrrSum 
                                           from child1__c where parentId__c In :parentIds Group By parentId__c];
                System.debug('--------Aggregate Result : '+ar);
                
                If(ar.size() > 0){
                    
                    for(AggregateResult res : ar){
                        
                        Id fsId = (Id)res.get('parentId__c');
                        fsOpp = fsOppMap.get(fsId);
                        
                        fsOpp.Trade_Count__c = (Integer)res.get('tradeCount');
                        System.debug('---Update Method-----Trade Count : '+fsOpp.Trade_Count__c);
                        
                        Decimal arr = (Decimal)res.get('arrSum');
                        System.debug('----Update Method----ARR : '+arr);
                        
                        Decimal nrr = (Decimal)res.get('nrrSum');
                        System.debug('----Update Method----NRR : '+nrr);
                        
                        fsOpp.Amount__c = arr + nrr ;
                        System.debug('----Update Method----Amount : '+fsOpp.Amount__c);
                        
                        mapToUpdate.put(fsOpp.Id,fsOpp);
                        
                    }
                }
                else
                {
                    for(Id ids : parentIds){
                        
                        fsOpp = new parent__c(id = ids); 
                        
                        fsOpp.Trade_Count__c = Null;
                        System.debug('----Update Method--Else Block--Trade Count : '+fsOpp.Trade_Count__c);
                        
                        fsOpp.Amount__c = 0;
                        System.debug('----Update Method--Else Block--Amount : '+fsOpp.Amount__c);
                        
                        mapToUpdate.put(fsOpp.Id,fsOpp);
                        
                    }
                }
                
            }
            
            Database.SaveResult[] res = Database.update(mapToUpdate.values(), false);
            
            
        }
        catch(Exception ex){
            System.debug('---Exception caught in Rollup Sum Update Method :'+ex.getLineNumber());
        }
        
    }
Please let me know any one..

Thanks,
Lakshmi
 
Steven NsubugaSteven Nsubuga
Hi Lakshmi, try this.
public static void rollupSumUpdate(Map<Id, child1__c> newRecs, Map<Id, child1__c> oldRecs){
   
        try{
            
            Set<String> parentIds = new Set<String>();
            
            for(Id chIds : newRecs.keySet()){
                
                child1__c tr = newRecs.get(chIds);
                
                if(tr.parentId__c != Null){
                    
                    parentIds.add(tr.parentId__c);
                    
                    System.debug('******If block -  Update Method - parentIds : **********'+parentIds);
					
					// add previous record just in case
					if (oldRecs.get(chIds).parentId__c != null) {
						parentIds.add(oldRecs.get(chIds).parentId__c);
					}
                }
                else
                {
                    parentIds.add(oldRecs.get(chIds).parentId__c);
                    System.debug('******else block -  Update Method - parentIds : **********'+parentIds);
                }
                
            }
            
            Map<Id,parent__c> mapToUpdate = new Map<Id,parent__c>();
            
            if(parentIds.size() > 0){
                
                Map<Id,parent__c> fsOppMap = new Map<Id,parent__c>([Select Id,Amount__c,Trade_Count__c from parent__c where Id In :parentIds]);
				
				// set the tradeCount to zero for all parents
				for (parent__c p : fsOppMap.values()) {
					p.Trade_Count__c = 0;
				}
                
                parent__c fsOpp = new parent__c();
                
                List<AggregateResult> ar = [Select parentId__c,Count(Id)tradeCount,Sum(Annual_Recurring_Revenue__c)arrSum,Sum(Non_Recurring_Revenue__c)nrrSum 
                                           from child1__c where parentId__c In :parentIds Group By parentId__c];
                System.debug('--------Aggregate Result : '+ar);
                
                If(ar.size() > 0){
                    
                    for(AggregateResult res : ar){
                        
                        Id fsId = (Id)res.get('parentId__c');
                        fsOpp = fsOppMap.get(fsId);
                        
                        fsOpp.Trade_Count__c = (Integer)res.get('tradeCount');
                        System.debug('---Update Method-----Trade Count : '+fsOpp.Trade_Count__c);
                        
                        Decimal arr = (Decimal)res.get('arrSum');
                        System.debug('----Update Method----ARR : '+arr);
                        
                        Decimal nrr = (Decimal)res.get('nrrSum');
                        System.debug('----Update Method----NRR : '+nrr);
                        
                        fsOpp.Amount__c = arr + nrr ;
                        System.debug('----Update Method----Amount : '+fsOpp.Amount__c);
                        
                        mapToUpdate.put(fsOpp.Id,fsOpp);
                        
                    }
                }
                else
                {
                    for(Id ids : parentIds){
                        
                        fsOpp = new parent__c(id = ids); 
                        
                        fsOpp.Trade_Count__c = Null;
                        System.debug('----Update Method--Else Block--Trade Count : '+fsOpp.Trade_Count__c);
                        
                        fsOpp.Amount__c = 0;
                        System.debug('----Update Method--Else Block--Amount : '+fsOpp.Amount__c);
                        
                        mapToUpdate.put(fsOpp.Id,fsOpp);
                        
                    }
                }
                
            }
            
            Database.SaveResult[] res = Database.update(mapToUpdate.values(), false);
            
            
        }
        catch(Exception ex){
            System.debug('---Exception caught in Rollup Sum Update Method :'+ex.getLineNumber());
        }
        
    }

 
Lakshmi SLakshmi S
Hi Steven ,

Thanks for your reply.
Not updated previoust parent opportunity count and amount values to zero.

Thanks
Lakshminarasimha
Steven NsubugaSteven Nsubuga
I have updated it.
public static void rollupSumUpdate(Map<Id, child1__c> newRecs, Map<Id, child1__c> oldRecs){
   
        try{
            
            Set<String> parentIds = new Set<String>();
            
            for(Id chIds : newRecs.keySet()){
                
                child1__c tr = newRecs.get(chIds);
                
                if(tr.parentId__c != Null){
                    
                    parentIds.add(tr.parentId__c);
                    
                    System.debug('******If block -  Update Method - parentIds : **********'+parentIds);
					
					// add previous record just in case
					if (oldRecs.get(chIds).parentId__c != null) {
						parentIds.add(oldRecs.get(chIds).parentId__c);
					}
                }
                else
                {
                    parentIds.add(oldRecs.get(chIds).parentId__c);
                    System.debug('******else block -  Update Method - parentIds : **********'+parentIds);
                }
                
            }
            
            Map<Id,parent__c> mapToUpdate = new Map<Id,parent__c>();
            
            if(parentIds.size() > 0){
                
                Map<Id,parent__c> fsOppMap = new Map<Id,parent__c>([Select Id,Amount__c,Trade_Count__c from parent__c where Id In :parentIds]);
				
				// set the tradeCount to zero for all parents
				for (parent__c p : fsOppMap.values()) {
					p.Trade_Count__c = 0;
				}
                
                
                List<AggregateResult> ar = [Select parentId__c,Count(Id)tradeCount,Sum(Annual_Recurring_Revenue__c)arrSum,Sum(Non_Recurring_Revenue__c)nrrSum 
                                           from child1__c where parentId__c In :parentIds Group By parentId__c];
                System.debug('--------Aggregate Result : '+ar);
                
                If(ar.size() > 0){
                    
                    for(AggregateResult res : ar){
                        
                        Id fsId = (Id)res.get('parentId__c');
                        
                        fsOppMap.get(fsId).Trade_Count__c = (Integer)res.get('tradeCount');
                        System.debug('---Update Method-----Trade Count : '+fsOppMap.get(fsId).Trade_Count__c);
                        
                        Decimal arr = (Decimal)res.get('arrSum');
                        System.debug('----Update Method----ARR : '+arr);
                        
                        Decimal nrr = (Decimal)res.get('nrrSum');
                        System.debug('----Update Method----NRR : '+nrr);
                        
                        fsOppMap.get(fsId).Amount__c = arr + nrr ;
                        System.debug('----Update Method----Amount : '+fsOppMap.get(fsId).Amount__c);
                        
                        
                    }
                }
                
            }
            
            Database.SaveResult[] res = Database.update(fsOppMap.values(), false);
            
            
        }
        catch(Exception ex){
            System.debug('---Exception caught in Rollup Sum Update Method :'+ex.getLineNumber());
        }
        
    }