0

I have a formula field on Account to calculate survey score. I want to calculate Average of all child account + Parent account survey scores and show that Avg. survey score on Parent account.

I have a trigger but it is showing only 1 child account survey score. Please help me to figure out the code.

trigger childaccount on Account (after update) 
{
Set<String> SetParentId = new set<String>();
Decimal Num;
for (Account a : trigger.new)
{
    if(a.parentId != null)
    {
        SetParentId.add(a.parentId);
    }
}

if(SetParentId.size() > 0 )
{
    List<Account> lstParentAcc = [ Select NPS_Score__c, Group_NPS__c , (Select NPS_Score__c From ChildAccounts) From Account where id in :SetParentId ];    

    For (Account ac : lstParentAcc)
    {
        List<Account> lstChildAcc = ac.ChildAccounts;

        for( Account childAcc : lstChildAcc )
        {
            Num = childAcc.NPS_Score__c;
        }
        ac.Group_NPS__c = Num;
    }
    if(lstParentAcc.size() > 0 )
    {
        update lstParentAcc;
    }   
   }   
  }

How can I get avg. of all child account and parent account survey score.

1 Answer 1

1

Try using Rollup Helper- this can solve your requirement without writing code.Below is the link:

https://appexchange.salesforce.com/listingDetail?listingId=a0N30000009i3UpEAI

2
  • yes correct...@Abhijeet
    – Ramesh M
    Commented Jul 28, 2016 at 12:20
  • Please mark the answer as the solution if it worked for you so that others may benefit from it :)
    – Abhijeet
    Commented Jul 28, 2016 at 12:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .