+ Start a Discussion
John NeilanJohn Neilan 

Trigger Issue

Hello,

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, 

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();
        }
}

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

try using Ids.add(opp.AccountId);

All Answers

BalajiRanganathanBalajiRanganathan
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!