Skip to main content

Roll-up Summary from Child Accounts to Parent Account

Roll-up Summary from Child Accounts to Parent Account


Standard Salesforce Roll-Up summary is work for Master Detail’s relationships such as Opportunity Amount to Account. But the Total Opportunity Amount will not roll-up to the parent accounts. So here I archive that through following to methods.

There two methods

  1. Using Apex Trigger
  2. Using Flows and process builder

Using Apex Triggers

Salesforce Developers can code and archive this. Whenever the opportunity is created the amount is roll-up to account using standard Roll-Up Summary field.
Step1: Create Roll-up Summary custom field in Account field named as 'Total Opportunity Amount'.
Total Opportunity Amount roll-ups to all accounts level. In our case we want to roll-up Total Opportunity amount to the parent accounts.
Step2: Create new custom currency field on account with read only accessibility.
Step3: Write a trigger on Account to calculate the Roll-up summary all child accounts of Total Opportunity Amount
The trigger will fire wherever the Total Opportunity Amount is changed, that calculate the cumulative amount of all child and roll up to parents.
Account Trigger code snippets.
trigger childAccountRollupTrigger on Account (after insert,after update,after delete){
if(checkRecursive.runOnce()){
Set<Id> setParentAccId = new Set<Id>();
if(Trigger.isInsert || Trigger.isUpdate){
for(Account acc: Trigger.new) {
if(acc.ParentId!=null)
setParentAccId.add(acc.ParentId);
}
}
if(Trigger.isDelete){
for(Account acc_Old : Trigger.old){
   if(acc_Old.ParentId!=null)
      setParentAccId.add(acc_Old.ParentId);
   }
 }
Map<Id,Double> mapChildTotalAmount = new Map<Id,Double>();
for(AggregateResult agg: [Select ParentId, SUM(Child_Total_Amount__c) from Account where ParentId IN : setParentAccId group by ParentId ]){
    mapChildTotalAmount .put((Id)agg.get(‘ParentId’),(Double)agg.get(‘expr0’));
 }

List<Account> ParentListUpdate = new List<Account>();
for(Account acc : [Select Id, Child_Total_Amount__c from Account where Id IN :setParentAccId]){
    Double TotalSummaryAmount = mapChildTotalAmount .get(acc.Id);
    acc.Child_Total_Amount__c = TotalSummaryAmount ;
    ParentListUpdate .add(acc);
}
if(ParentListUpdate !=null && ParentListUpdate.size()>0)
update ParentListUpdate ;
}

}



Using Flow and Process builder

The Administrator can also implements it easily without writing single line of code. Here we can create flow to calculate the sum of the child total amount. And create a process to call the flow when Total Opportunity Amount is changed.
Let’s start to create Flow.
Step 1: Fast lookup to query all Child accounts related to parent account, Drage & Drop — Fast lookup into empty canvas. This is the first element in flow.
Fill the name and unique name. In Filters and Assignments section, select our child Account name as lookup. Now, we will set criteria’s to query child records. You can set criteria based on your requirements. Here in Field box select the lookup field parentId. To lookup all related Employees to specific Company we have to define a variable (parentIds) which will pass by Process Builder.
To lookup all related Employees to specific Company we have to define a variable (parentids) which will pass by Process Builder.
Give Unique Name, Data Type. Select Input/Output Type to access this variable later in Process Builder.
Create one more variable to store the lookup query result.
In step 1 we have passed the parentid from process builder to query related child account records and stored the query results.
Step 2: Drage & Drop the Loop element from palette to iterate the records to do summary
To perform the logic inside the loop we need to specify a variable.
We need a variables to calculate Sum of Total Opportunity Amount for the Parent Account.
Step 3: Calculate Sum of Child Total Amount using Assignment Logic. Drag and Drop the assignment element onto the canvas.
Step 4: Update the Child Total Amount to Parent record.
Step 5: Make connections between the elements. We have finished with logic and have to connect the elements to make a flow as in below diagram.
Step 6: Call flow from Process Builder
Now, we have to call this flow and pass parent Id from process builder when on create/update of child account record.

Comments

Popular posts from this blog

Configure Lightning for Outlook

Introduction            Salesforce Lightning for outlook helps Salesforce users get more work done without switching between Outlook and Salesforce. The Salesforce users can perform apps specific actions more effectively. The Salesforce users can create Salesforce records using global actions—directly in their email applications. Specifications             As per Salesforce implementation guide, it works on outlook 2013 and 2016, but not specified about the version of Operating System. During our implementation, we absorbed that it works on windows 10 Operating System. It's required TLS 1.1 or higher version. Present Features ·          Lightning for outlook will work on outlook web app and MS office outlook client ·          Can search for Salesforce records inside Outlook ·          Automatically sync contacts and calendar events between Outlook and Salesforce ·          Open Salesforce records in read mode inside Outlook ·          Using Email recipient

Salesforce Certified Platform Developer I - Spring '16 Release Exam

Salesforce Certifications Release Exams I have cleared the Salesforce Certified Platform Developer I on winter 16 and I supposed clear the release exam for Spring 16. First attempt I had failed due two wrong answers out 5 questions. Time limit is 30 Mins. The second attempt I have cleared the exam. There are 3 attempt is limit. I got the following questions, What is the optimal way to verify a user before showing them sensitive content?    A.  Calling the Session.forcedLoginUrl method in apex. B.  Sending the user a SMS message with a passcode. C.  Sending the user an email message with a passcode. D.  Calling the generateVerificationUrl method in apex. What actions types should be configured to display a custom success message? A.  Post a feed item. B.  Close a case. C.  Update a record. D.  Delete a record. What are the supported content sources for custom buttons and links?   Choose 2 answers A.  Static Resource B.  Lightning Page C.  Chatter F