3

I have a custom field in my account called Priority (picklist) I have a lookup to Account in a custom object called Customer. I want to access Account.Priority but it always returns null even though I made 100% certain that the pick-list is filled in.

for (Customer__c c : Trigger.new) {
        //Customer__c has one field, invisible to all viewers, that is filled with the uniqueID (Title FirstName LastName). It is then checked against the database to see if there are entries with the same uniqueID
        c.Unique_Id__c    = c.Title__c + ' ' + c.First_Name__c + ' ' + c.Last_Name__c;

        //debugging purposes: Check to see if priority has a value stored inside.
        c.Address__c = 'priority: ' + c.Account__r.Priority__c;

        customers.put(c.Unique_Id__c, c);        
    }

Any ideas what I'm doing wrong here? Thanks in advance.

1 Answer 1

2

In Trigger Context Collections (new, old, newMap, oldMap), only the immediate fields of the trigger object have values. So in your case only the fields of the Customer Object have values. Lookups and Master Details have the ID, but they do not have the related fields populated (Basically, any field that uses __r). They always return null, which is why your priority value is always coming back as null.

To work around this you need to utilize a Set or Map to query and hold these values so you can later access them in your trigger.

Something like below should work to populate your map with a single query

map<Id,Account> accountMap = new map<Id,Account>();
for (Customer__c c : Trigger.new){
     accountMap.put(c.Account__c, null);
}
accountMap.remove(null);
accountMap.putAll([Select Id, Name, Priority__c From Account Where Id In : accountMap.keyset()]);

Then you can access the map to get the priority of the customer account like this

for(Customer__c c : Trigger.new) {
    c.Address__c = 'priority: ' + accountMap.get(c.Account__c).Priority__c;      
}
2

You must log in to answer this question.

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