I need to copy the total Opportunities [Closed won] Amount to Custom object's field. Below is the full criteria.


Operator Program, Operator Applicable Program, Accounts, Opportunity


Operator Program and Accounts are master detail to Operator Applicable program.

I need to calculate and represent Operator Applicable program's [Accounts] Opportunities [Closed won] Amount at Operator Program level.

I could able to copy field value from Account level but couldn't able to calculate opp's value. Below is the code that tried.

trigger totalopamount on Operator_Applicable_Accounts__c (before update, after insert, after update, after delete) {

Map<id, List<Operator_Applicable_Accounts__c>> opacidlistMap = new Map<id, List<Operator_Applicable_Accounts__c>>();
Map<id, List<Account>> acidlistMap = new Map<id, List<Account>>();
Map<Id, List<Opportunity>> oplistMap = new Map<id, List<Opportunity>>();
set<id> opids = new set<Id>();
List<Operator_Applicable_Accounts__c> oaalist = new List<Operator_Applicable_Accounts__c>();
List<Opportunity> accoplist = new List<opportunity>();
if(trigger.isinsert || trigger.isupdate) {
    for(Operator_Applicable_Accounts__c o : trigger.new) {
        if(o.Operator_Tab__c != null) {

    for(Operator_Applicable_Accounts__c o : trigger.old) {
        if(o.Operator_Tab__c != null) {

if(opids.size()>0) {
    oaalist = [Select Name, Total_Opp_Amount__c, Operator_Tab__c, Account_Name__r.Name From Operator_Applicable_Accounts__c WHERE Operator_Tab__c in : opids AND Account_Name__r.Name in : opids];
    accoplist = [Select Id, Name, Amount, CreatedDate FROM Opportunity WHERE Accountid in : opids]; 
    for(Operator_Applicable_Accounts__c oa : oaalist) {
        if(oa.Total_Opp_Amount__c != null) {
            opacidlistMap.put((oa.Operator_Tab__c), new List<Operator_Applicable_Accounts__c>());
    List<Operator_Program__c> oplist = new List<Operator_Program__c>();
    oplist = [Select Total_Opp_Amount_since_operator__c from Operator_Program__c where id in :opids];
    for(Operator_Program__c po: oplist) {
        List<Operator_Applicable_Accounts__c> templist = new List<Operator_Applicable_Accounts__c>();
        templist = opacidlistMap.get(po.id);
        Double totalopamount = 0;
        for(Operator_Applicable_Accounts__c aao: templist) {
        if(aao.Total_Opp_Amount__c != null){
            totalopamount += aao.Total_Opp_Amount__c;
        po.Total_Opp_Amount_since_operator__c = totalopamount;
       update oplist;

  • 2
    what exactly the issue you are facing? Aug 26 '16 at 20:30

I don't know on what basis your records are created, but it would seem that you need two different kinds of triggers here. One I'd expect to be for AfterInsert although you could also do it on BeforeInsert if you really had to. That one would fire to collect all of your related Accounts and Closed-won Opportunity Amounts which you can get in a single query. You could also update those Accounts and ALL Opps whether closed-won or not with the detail recordIDs of the Master Object record at the same time.

The same trigger would seem to need to fire on BeforeDelete to remove the associations of all related Accounts and Opportunities simply to clear that field on each of them and perform the update to Account and Opportunity.

You then would seem to need another trigger that fires AfterUpdate on Opportunity when the status changes to closed-won where the previous value != closed-lost or another value that doesn't make sense. That trigger would update the Master Object record's related list of closed-won opportunities or roll-up on their amounts if that's what you have (your question is unclear what you want on that). The trigger would also need to check to see if there is a Master Record first because of your Before Delete Trigger and since some Opps may not be associated with a Master Record for other reasons.

Also, you should only need a single query on Opp for this since Opp will contain the AcctId. If you need more info than that like the Acct.Name, just include it in a subquery.

I think this should help you sort your issues out and get you pointed in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.