+ Start a Discussion
niharnihar 

How to count all opportunity records(child records) Amount field values and display in account record(parent record)

My Requirment :
Is to count all amount field values in all opportunity records and show in seperate field in  account record(parent record)

As per my client i must do by using triggers only

How can i achieve it

can anyone help writing trigger for oppoartunity records on account 

Thanks InAdvance.....................
Best Answer chosen by nihar
balaji Jayararmanbalaji Jayararman
Hi nihar,

Please create a custom field(OpportunityTotalAmount__c) on Account Object and below is the trigger code that you are looking for

trigger TotalAmountOnAccount on Opportunity (after insert, after update) {
    Set<Id> accountIdSet = new Set<Id>();
    if (Trigger.isInsert) {
        for (Opportunity oppr : Trigger.new) {
            accountIdSet.add(oppr.AccountId);
        }
    } else if (Trigger.isUpdate) {
        for (Opportunity oppr : Trigger.new) {
            if (Trigger.oldMap.get(oppr.Id).Amount != oppr.Amount) {
                accountIdSet.add(oppr.AccountId);
            }
        }
    }
    if (accountIdSet.size() == 0) {
        return;
    }
    List<Account> accountList = [
                    SELECT
                        Id,
                        (SELECT
                            Id, Amount
                        FROM
                            Opportunities
                        )
                    FROM
                        Account
                    WHERE
                        Id IN :accountIdSet
                ];
    Map<Id, Account> updateAccountMap = new Map<Id, Account>();
    for (Account acc : accountList) {
        Decimal totalAmount = 0;
        for (Opportunity oppr : acc.Opportunities) {
            totalAmount += oppr.Amount;
        }
        acc.OpportunityTotalAmount__c = totalAmount;
        updateAccountMap.put(acc.Id, acc);
    }
    if (!updateAccountMap.isEmpty()) {
        update updateAccountMap.values();
    }

}

Please mark this answer as correct. if it helps.

Regards,
Balaji Jayaranab
 

All Answers

balaji Jayararmanbalaji Jayararman
Hi nihar,

Please create a custom field(OpportunityTotalAmount__c) on Account Object and below is the trigger code that you are looking for

trigger TotalAmountOnAccount on Opportunity (after insert, after update) {
    Set<Id> accountIdSet = new Set<Id>();
    if (Trigger.isInsert) {
        for (Opportunity oppr : Trigger.new) {
            accountIdSet.add(oppr.AccountId);
        }
    } else if (Trigger.isUpdate) {
        for (Opportunity oppr : Trigger.new) {
            if (Trigger.oldMap.get(oppr.Id).Amount != oppr.Amount) {
                accountIdSet.add(oppr.AccountId);
            }
        }
    }
    if (accountIdSet.size() == 0) {
        return;
    }
    List<Account> accountList = [
                    SELECT
                        Id,
                        (SELECT
                            Id, Amount
                        FROM
                            Opportunities
                        )
                    FROM
                        Account
                    WHERE
                        Id IN :accountIdSet
                ];
    Map<Id, Account> updateAccountMap = new Map<Id, Account>();
    for (Account acc : accountList) {
        Decimal totalAmount = 0;
        for (Opportunity oppr : acc.Opportunities) {
            totalAmount += oppr.Amount;
        }
        acc.OpportunityTotalAmount__c = totalAmount;
        updateAccountMap.put(acc.Id, acc);
    }
    if (!updateAccountMap.isEmpty()) {
        update updateAccountMap.values();
    }

}

Please mark this answer as correct. if it helps.

Regards,
Balaji Jayaranab
 
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Nihar,

please refer below code.

//Apex Class:-

public class SumofAmount 
{
 public static void sumethod(List<opportunity> opplist)
 {
     set<Id> setid=new set<Id>();
     for(opportunity op:opplist)
     {
        setid.add(op.AccountId);
     }
      List<Account> Acclist=new List<Account>();
     for(Account a:[Select name from Account where Id In:setid])
     {
         Decimal count=0;
         for(opportunity opp:opplist)
         {
                          count+=opp.Amount;
          }
         a.TotalAmount__c=count;
             Acclist.add(a);
     }
     update Acclist;
 }
}
//Trigger:-
trigger UpdateChildObjectFieldTrg on Opportunity (after insert, before update)
{
for(opportunity o:trigger.new)
 {
     if(o.AccountId!=null)
     {
         SumofAmount.sumethod(trigger.new);
     }
 }
}

You have to create a custom field on an account, like "TotalAmount".

Please mark it as best answer if it really helps you.

Thank You,
Ajay Dubedi