You need to sign in to do that
Don't have an account?
Jon Jax
Using Apex best practices, what trigger context is best used to update a list of parent records with aggregated results from their respective children, given the trigger is invoked with a parent insert/update as well as a child insert/update?
I'm running into a tough issue where the business case involves parent and children opportunities. There are a few summary fields on the parent that need to be updated based on the sum values of the children. However, if the parent is updated on any of the summary fields, the summary function is disabled (via boolean field) and will not summarize again unless the checkbox is marked false by a system admin. Just as well, upon deletion of a child, the parent will re-summarize.
I realize process builder is a viable option for some of these functions, but I'd like to write it completely in apex. Also, roll-up summary fields are not an option, as it is not a master-detail relationship.
I'm currently attempting to use an aggregate soql function to calculate, which works very well, but bulk updating the parent records does not take and I am not sure which trigger context I can even call this from. Any help would be appreciated, attached is the summing method I am currently working with:
I realize process builder is a viable option for some of these functions, but I'd like to write it completely in apex. Also, roll-up summary fields are not an option, as it is not a master-detail relationship.
I'm currently attempting to use an aggregate soql function to calculate, which works very well, but bulk updating the parent records does not take and I am not sure which trigger context I can even call this from. Any help would be appreciated, attached is the summing method I am currently working with:
//passes in list of parent opportunity Ids to process public static void sumParentOpp(List<Id> p) { System.debug('In sumParentOpp method: ' + p); List<Opportunity> oppsToUpdate = new List<Opportunity>(); if(p.size() > 0) { List<Opportunity> parentSumOpps = [SELECT Id, Disable_Summarizing_From_Child__c FROM Opportunity WHERE Id IN : p AND Disable_Summarizing_From_Child__c = FALSE]; System.debug('Parent Opps that allow summary: ' + parentSumOpps); if(parentSumOpps.size() > 0){ List<AggregateResult> childOppSummary = [SELECT Parent_Opportunity__c pId, SUM(SumField1__c) sF1, SUM(SumField2__c) sF2, SUM(SumField3__c) sF3 FROM Opportunity WHERE Parent_Opportunity__c IN : parentSumOpps GROUP BY Parent_Opportunity__c]; System.debug('Child Opp Summarys: ' + childOppSummary); Map<Id,Opportunity> summedParent = new Map<Id,Opportunity>(); for(AggregateResult ar : childOppSummary){ Opportunity pOpp = new Opportunity(); pOpp.Id = (Id)ar.get('pId'); pOpp.Pipeline_kW__c = (Decimal)ar.get('sF1'); pOpp.Pipeline_System_Size_kWh__c = (Decimal)ar.get('sF2'); pOpp.New_Solar_Pipeline_Size_kW__c = (Decimal)ar.get('sF3'); summedParent.put(pOpp.Id, pOpp); System.debug('Summed Parent Opp Created with Sum Values: ' + pOpp); } for(Opportunity parentOpp : parentSumOpps){ Opportunity sParent = summedParent.get(parentOpp.Id); parentOpp.Pipeline_kW__c = sParent.Pipeline_kW__c; parentOpp.Pipeline_System_Size_kWh__c = sParent.Pipeline_System_Size_kWh__c; parentOpp.New_Solar_Pipeline_Size_kW__c = sParent.New_Solar_Pipeline_Size_kW__c; System.debug('Parent Opp Values Updated: ' + parentOpp); } } if(oppsToUpdate.size() > 0){ update oppsToUpdate; System.debug('Updated Opps: ' + oppsToUpdate.size()); } } }
you are not adding any records oppsToUpdate list.
Please check following URLs for roll-up summary functionality.
http://salesforceprasad.blogspot.in/
https://ck-developer-salesforce-com.360casb.com/forums/ForumsMain?id=9060G0000005N9lQAE
Hope this helps you!
Thanks
Varaprasad
For Support: varaprasad4sfdc@gmail.com