0

See the code below from an APEX trigger. For some reason, my trigger always reads nextRenewalDate (field from Account object) as null, regardless of whether or not it is blank. Racking my brain here and can't find a solution online. Help!

trigger updateCloseDate on Opportunity (before insert, before update) {
    for (Opportunity newOpp : Trigger.new) {
        Opportunity opp = new Opportunity();

        // New variable referencing the related Account field'Next Renewal Date' 
        Date nextRenewalDate = newOpp.Account.Next_Renewal_Date__c;

        if (newOpp.RecordTypeId == '<<recordID>>') {

            // if the related Account "Next Renewal Field" is not blank
            if (nextRenewalDate == null) {
                newOpp.CloseDate = date.today() + 60;                              
            }
            if (nextRenewalDate != null) {
                newOpp.CloseDate = nextRenewalDate - 60;
            }
            break;
        }   

    }

}

Adding the updated code here (still not working properly):

trigger closeDateTest on Opportunity (before insert) {
Opportunity newOpp = new Opportunity();

Set<Id> parentAccountIds = new Set<Id>();
for(Opportunity opp :Trigger.new){
    parentAccountIds.add(opp.AccountId);

    system.debug('parentAccountId assigned');

    //query fields on parent accounts and map them by Account Id
    Map<Id, Account> parentAccountsById = new Map<Id, Account>([
        select Id, Next_Renewal_Date__c
        from Account
        where Id in :parentAccountIds
        ]); 

    system.debug('SOQL Query completed');

    if(String.isNotBlank(newOpp.AccountId)) {
        Date nextRenewalDate = parentAccountsById.get(newOpp.AccountId).Next_Renewal_Date__c;

        system.debug('nextRenewalDate assigned');

        // if the Opportunity Record ID is 'New to Blue'
        if (newOpp.RecordTypeId == SObjectType.Opportunity.getRecordTypeInfosByDeveloperName()
.get('New_to_Blue').getRecordTypeId()) {

            system.debug('checked record type');

            // if the related Account "Next Renewal Field" is not blank
            if (nextRenewalDate == null) {

                // add 60 days to Close Date
                newOpp.CloseDate = date.today() + 60; 
                newOpp.NextStep = 'is BLANK';
            }

            if (nextRenewalDate != null) {
                // subtract 60 days from Next Renewal Date
                newOpp.CloseDate = nextRenewalDate - 60;
                newOpp.NextStep = 'is FILLED IN';
            }

        } 

    }

}

}

THE SOLUTION:

trigger closeDateTest on Opportunity (before insert) {
    Opportunity newOpp = new Opportunity();

    Set<Id> parentAccountIds = new Set<Id>();
    for(Opportunity opp :Trigger.new){
        parentAccountIds.add(opp.AccountId);

        //query fields on parent accounts and map them by Account Id
        Map<Id, Account> parentAccountsById = new Map<Id, Account>([
            select Id, Next_Renewal_Date__c
            from Account
            where Id in :parentAccountIds
        ]); 

        for (Opportunity newOpp : Trigger.new) {

            if(String.isNotBlank(newOpp.AccountId)) {
                Date nextRenewalDate = parentAccountsById.get(newOpp.AccountId).Next_Renewal_Date__c;

                // if the Opportunity Record ID is 'New to Blue'
                if (newOpp.RecordTypeId == SObjectType.Opportunity.getRecordTypeInfosByDeveloperName()
    .get('New_to_Blue').getRecordTypeId()) {

                    system.debug('checked record type');

                    // if the related Account "Next Renewal Field" is not blank
                    if (nextRenewalDate == null) {

                        // add 60 days to Close Date
                        newOpp.CloseDate = date.today() + 60; 
                    }

                    if (nextRenewalDate != null) {
                        // subtract 60 days from Next Renewal Date
                        newOpp.CloseDate = nextRenewalDate - 60;
                    }

                } 

            }

        }

    }

}
  • 1
    Found the solution with a some help from @Oleksandr. His solution is not correct but it helped quite a bit. Giving him a an upvote and posting the solution to the original post. – paulK Oct 31 '18 at 20:24
2

Because parent fields are not accessible in trigger context. You have to explicitly query newOpp.Account.Next_Renewal_Date__c field.

trigger updateCloseDate on Opportunity (before insert, before update) {

    Set<Id> parentAccountIds = new Set<Id>();
    for(Opportunity opp :Trigger.new){
        parentAccountIds.add(opp.AccountId);
    }

    //query fields on parent accounts and map them by Account Id
    Map<Id, Account> parentAccountsById = new Map<Id, Account>([
        select Id, Next_Renewal_Date__c
        from Account
        where Id in :parentAccountIds
        ]);

    for (Opportunity newOpp : Trigger.new) {
        Opportunity opp = new Opportunity();

        // New variable referencing the related Account field'Next Renewal Date' 
        if(String.isNotBlank(newOpp.AccountId)){
            Date nextRenewalDate = parentAccountsById.get(newOpp.AccountId).Next_Renewal_Date__c;

            // if the Opportunity Record ID is 'New to Blue'
            //avoid hardcoding ids in code newOpp.RecordTypeId == '0121N000001AR44QAG'

            if (newOpp.RecordTypeId == SObjectType.Opportunity.getRecordTypeInfosByDeveloperName()
.get('New_to_Blue').getRecordTypeId()) {

                // if the related Account "Next Renewal Field" is not blank
                if (nextRenewalDate == null) {
                    newOpp.CloseDate = date.today() + 60;                              
                }
                if (nextRenewalDate != null) {
                    newOpp.CloseDate = nextRenewalDate - 60;
                }
            } 
        }       
    }

}
  • Ok. I thought I was doing that by assigning newOpp.Account.Next_Renewal_Date__c to the variable nextRenewalDate. How should I be coding it to make it explicit? – paulK Oct 31 '18 at 17:19
  • @paulK I have updated the answer with code. – Oleksandr Berehovskyi Oct 31 '18 at 17:20
  • Oh nice. I'm working on this now. Will update with the best solution once I hammer out another bug. – paulK Oct 31 '18 at 17:36
  • Hey @Oleksandr, this isn't working and I'm getting no errors. After testing, I can confirm the trigger is not reaching the last two IF statements at all. – paulK Oct 31 '18 at 18:02
  • Could this be an issue of grabbing the AccountID but it being a 'before' trigger? – paulK Oct 31 '18 at 18:03

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