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
John NeilanJohn Neilan 

Trigger Issue


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 Oppotunity, 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, 

ClassOpp2Account updater14 = new ClassOpp2Account();

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)
     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){


                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;
        update acctMap.values();

Best Answer chosen by John Neilan
Instead of Ids.add(opp.Account.Id);

try using Ids.add(opp.AccountId);

All Answers

Instead of Ids.add(opp.Account.Id);

try using Ids.add(opp.AccountId);
This was selected as the best answer
John NeilanJohn Neilan
Thanks!  That did it!