function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sumanth Kumar 17Sumanth Kumar 17 

Trigger for Account amount field that to be updated based on related opportunity amount field

Hello Developers....I have a task where Account amount field to be updated based on opportunity amount field. I have total value field in account and for its related opportunity, if we enter amount for those opportnities automatically the total value field in the account should be updated. I need to write a trigger for that. Can anyone help me with this task...
Thanks in advance
Sumanth kumar
Best Answer chosen by Sumanth Kumar 17
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Sumanth,

You can use below trigger:

trigger Opportunity_AIUD on Opportunity (after insert, after update, after delete) {
    Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
    Set<Id> acctIds = new Set<Id>();
    List<Opportunity> opptyList = new List<Opportunity>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Opportunity oppty : trigger.New){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Opportunity oppty : trigger.old){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
            }
        }    
    }
    if(acctIds.size() > 0){
        opptyList = [SELECT Amount, AccountId FROM Opportunity WHERE AccountId IN : acctIds];
        for(Opportunity oppty : opptyList){
            if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
                acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
            }
            acctIdOpptyListMap.get(oppty.AccountId).add(oppty); 
        }   
        List<Account> acctList = new List<Account>();
        acctList = [SELECT Total_Opportunity_Amount__c FROM Account WHERE Id IN: acctIds];
        for(Account acct : acctList){
            List<Opportunity> tempOpptyList = new List<Opportunity>();
            tempOpptyList = acctIdOpptyListMap.get(acct.Id);
            Double totalOpptyAmount = 0;
            for(Opportunity oppty : tempOpptyList){
                if(oppty.Amount != null){
                    totalOpptyAmount += oppty.Amount;
                }
            }
            acct.Total_Opportunity_Amount__c = totalOpptyAmount;
        }
        update acctList;
    }
}

All Answers

Deepak Maheshwari 7Deepak Maheshwari 7

Hi Sumanth,

 

This can be achieved by making the Roll-up summary field on Account.

If you want to achieve it by trigger plz let me know.

 

Thanks

Sumanth Kumar 17Sumanth Kumar 17
Thanks Deepak for your concern..I need this task to be done using trigger...Can you please help me with this requirement.

Thanks
Sumanth Kumar
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Sumanth,

You can use below trigger:

trigger Opportunity_AIUD on Opportunity (after insert, after update, after delete) {
    Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
    Set<Id> acctIds = new Set<Id>();
    List<Opportunity> opptyList = new List<Opportunity>();
    if(trigger.isUpdate || trigger.isInsert){
        for(Opportunity oppty : trigger.New){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
            }
        }    
    }
    if(trigger.isDelete){
        for(Opportunity oppty : trigger.old){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
            }
        }    
    }
    if(acctIds.size() > 0){
        opptyList = [SELECT Amount, AccountId FROM Opportunity WHERE AccountId IN : acctIds];
        for(Opportunity oppty : opptyList){
            if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
                acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
            }
            acctIdOpptyListMap.get(oppty.AccountId).add(oppty); 
        }   
        List<Account> acctList = new List<Account>();
        acctList = [SELECT Total_Opportunity_Amount__c FROM Account WHERE Id IN: acctIds];
        for(Account acct : acctList){
            List<Opportunity> tempOpptyList = new List<Opportunity>();
            tempOpptyList = acctIdOpptyListMap.get(acct.Id);
            Double totalOpptyAmount = 0;
            for(Opportunity oppty : tempOpptyList){
                if(oppty.Amount != null){
                    totalOpptyAmount += oppty.Amount;
                }
            }
            acct.Total_Opportunity_Amount__c = totalOpptyAmount;
        }
        update acctList;
    }
}

This was selected as the best answer
Sumanth Kumar 17Sumanth Kumar 17
Hi Deppak...it is showing following error 
Error: Compile Error: Incorrect SObject type: Opportunity should be Account at line -1 column -1
Can you please fix this?
Deepak Maheshwari 7Deepak Maheshwari 7

Are you writing trigger on Account?

Please write it on Opportunity

Sumanth Kumar 17Sumanth Kumar 17
Ya...It was my mistake, but your answer was spot on..Thanks for your Concern.

Cheers
Sumanth Kumar
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Sumanth,

 

Did your requirement was not fulfilled by the above trigger?

Please let me know 

Sumanth Kumar 17Sumanth Kumar 17
Hi Deepak...your answer fulfilled my requirement. Its working fine!

Thanks
Sumanth Kumar
Sumanth Kumar 17Sumanth Kumar 17
Hi Deepak, I have a question for you...Can you please explain me what is AccountId in the above code and what is the function of map and set w.r.t the code.

Thanks
SUmanth Kumar
Deepak Maheshwari 7Deepak Maheshwari 7

Hi Sumanth,

 

1.) Here AccountId is lookup field of Account on Opportunity.

2.) Set is used for storing Id of Account corresponding to each Opportunity.

3.) Map is key-value pair. Here key is AccountId and value is List of Opportunities corresponding to that AccountId.

 

If you have any more query then you can drop me mail on below address:

 

dpkm20@gmail.com

 

Sumanth Kumar 17Sumanth Kumar 17
Hi Deepak..
I have following doubts regarding that trigger.
1) I do not have AccountID lookup field on Opportunity. But how did the trigger getting fired without that field.
2) In Map<Id, List<Opportunity>> and  Set<Id>, are both ID's used in set and map are same?
3) As you said that key used in map is AccountId, is ID are AccountID same?

sorry for more queries as am in process of understanding each and every line of code.
Thanks
Sumanth kumar