1

I have an after trigger that fires when an Opportunity is updated. I'm having an issue because when I look for the Account ID related to the Opportunity, it is returning as Null even though the Opportunity is absolutely linked to an Account (system debug at line 25). All the other variables in the debug statement return as expected. Does anyone know why the Account field does not? Thanks,

Trigger:

ClassOpp2Account updater14 = new ClassOpp2Account();
            updater14.mgdUpdates(Trigger.new);

Trigger Class:

public class ClassOpp2Account{

    public void mgdUpdates(List<Opportunity> MgdAcctUpdates){

    Map<Id,Account> acctMap = new Map<Id,Account>();
    Set<id> Ids = new Set<id>();

    String recordTypeName = 'Renewals';
    Map<String,Schema.RecordTypeInfo> rtMapByName = Schema.SObjectType.Opportunity.getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtInfo =  rtMapByName.get(recordTypeName);
    id recType = rtInfo.getRecordTypeId();

    List<Account> accountsToUpdate = new List<Account>();
        for (Opportunity opp : MgdAcctUpdates)
        {
                Ids.add(opp.Account.Id);
            }

     Map<Id,Account> acctMap2 = new Map<Id,Account>([SELECT Id,Name,Managed_Renewal_Date__c,Managed_Term__c,Managed_Value__c
                                                    FROM Account
                                                    WHERE Id in :Ids]);

        for (Opportunity opp2 : MgdAcctUpdates){

system.debug('***#*** IsClosed: '+opp2.IsClosed+' Stage: '+opp2.StageName+' Opp Rec Type: '+opp2.RecordTypeId+' Rec Type: '+recType+' Acct Name: '+opp2.Account);
            if(opp2.RecordTypeId == recType && opp2.IsClosed == true){

            if(acctMap2.containsKey(opp2.Account.Id)){

                Account acct = acctMap2.get(opp2.Account.Id);
            if(acctMap2.get(opp2.Account.Id).Managed_Renewal_Date__c < opp2.Renewal_Date_Next__c || acctMap2.get(opp2.Account.Id).Managed_Renewal_Date__c == null) {
                    acct.Managed_Renewal_Date__c = opp2.Renewal_Date_Next__c;
                    acct.Managed_Term__c = opp2.Term__c;
                    acct.Managed_Value__c = opp2.Final_Annualized_Amount__c;
            }
                 acctMap.put(acct.id,acct);
            }
            }
        }
        update acctMap.values();
        }
}
  • You say that the trigger isn't firing, but it is... because you are seeing the debug output from line 25. What is it that's not working correctly in your code? What do you expect it to be doing? – Mark Pond Mar 3 '15 at 21:27
2

I suspect that if you change this syntax in your trigger everywhere that it is written this way:

opp2.Account.Id

To

opp2.AccountId

it will work as you expect.

The data which is in the trigger collection is just the information on the Opportunity records themselves not any related data. Your current syntax is traversing from the Opportunity object up to the Account object through the relationship between them.

If you change the syntax to just use the AccountId field directly on the Opportunity, you'll likely find a non-null value there.

  • 1
    You're absolutely right. Relationships aren't automatically populated, so Account.Id isn't the same as AccountId. This is probably the easiest trap to fall in to or second guess yourself on. – sfdcfox Mar 3 '15 at 22:09

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.