You need to sign in to do that
Don't have an account?
Lakshmi 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 :
----------
Thanks,
Lakshmi
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
Thanks for your reply.
Not updated previoust parent opportunity count and amount values to zero.
Thanks
Lakshminarasimha