1

I'm trying to write some APEX to get the Owner of an Account (Account__c) referenced by a Lead and set that Owner as the Lead Owner.

I'm running into an error where the variable contains a null value, but I'm not seeing why.

Relevant portions of code below.

Trigger:

trigger LeadTrigger on Lead (before insert, before update, after insert, after update) {

if (trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) {

    System.debug('Starting LeadHelper.companyLeadAccountChange');
    LeadHelper.companyLeadAccountChange(trigger.new, trigger.oldMap);
    System.debug('Ending LeadHelper.companyLeadAccountChange');
}
}

Class:

public static void companyLeadAccountChange (list<Lead> company, map<Id, Lead> mapCompany) {
    If (company != null && !company.isEmpty()){
        
        For(Lead i : company){
            System.debug('Account: '+i.Account__c+', Account Owner (lead relation): '+i.Account__r.OwnerId+', Account Owner (account lookup): '+[SELECT Id, Name, OwnerId, Owner.Name
                                                                                                                                                 FROM Account
                                                                                                                                                 WHERE Id =:i.Account__c]);

            If(i.recordTypeId == Util.getRecordTypeId('Lead','Client') &&
               i.Account__c != NULL && 
               (trigger.isInsert || (trigger.isUpdate && 
                                     i.Account__c != mapCompany.get(i.Id).Account__c))){
                                         
                                         System.debug('Current: ' + i.OwnerId + ', New: ' + i.Account__r.OwnerId);
                                         i.OwnerId = i.Account__r.OwnerId;
                                     }
        }
    }

}

Here are some of the debug lines:

USER_DEBUG|[123]|DEBUG|Account: 001M000000dFYdvIAG, Account Owner (lead relation): null, Account Owner (account lookup): (Account:{Name=Test, OwnerId=005M0000005zikzIAA, Id=001M000000dFYdvIAG})

USER_DEBUG|[132]|DEBUG|Current: 00GC0000002BekVMAS, New: null

For some reason, the lead's Account__c field is returning a value, but looking through the relationship lookup returns a null owner of that Account. You can see in the Debug that the Query returns an owner of that specific record. Can anyone identify why the account returns owner ID when queried but returns null when using the relationship reference? Should I just use the i.Account__c to instantiate an account record and get the ownerId that way?

1 Answer 1

1

Relationship fields - ones that end in __r - are not populated in triggers. They are only populated when you perform explicit relationship queries.

So yes you will need to query for the Account.OwnerId values using the Account ID values in the Lead.Account__c field. The resulting method would look something like this:

public static void companyLeadAccountChange (list<Lead> company,
        map<Id, Lead> mapCompany) {
    Set<Id> accountIds = new Set<Id>();
    Lead[] leads = new Lead[] {};
    Id clientRtId = Util.getRecordTypeId('Lead','Client');
    For(Lead l : company){
        If(l.recordTypeId == clientRtId && ...){
            accountIds.add(l.Account__c);
            leads.add(l);
        }
    }
    If (accountIds.size() > 0) {
        Map<Id, Account> accounts = new Map<Id, Account>([
                select OwnerId
                from Account
                where Id in :accountIds
                ]);
        For(Lead l : leads) {
            l.OwnerId = accounts.get(l.Account__c).OwnerId;
        }
    }
}
3
  • 1
    another in the long running series of 'why does my trigger return null for related sobjects'
    – cropredy
    Commented Nov 4, 2014 at 22:57
  • Thank you both - @crop1645, I saw another post similar to my own, and based on what you said; sounds like there has been a lot of frustration around this for people. Is there any word from Salesforce on if they are planning it or anything on the idea exchange?
    – zainogj
    Commented Nov 5, 2014 at 13:14
  • @zainogj I doubt this will ever change because querying one or more fields of related objects adds huge work to the trigger framework and where would that querying stop (as there can be many levels of relationships). A (versioned) change of the runtime behaviour to throw an exception that explains the problem better perhaps could be done.
    – Keith C
    Commented Nov 5, 2014 at 13:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .