0

Is it possible to fetch custom Account fields when AfterUpdate on Contact runs?

Example:

trigger myTrigger on Contact(after update) {
   for(Contact cont : [select id, Account.My_Field__c 
                      from Contact 
                      Where Id IN: Trigger.newMap.keySet()]) {
       // this always returns null
       system.debug('account.my_field__c value: ' + cont.Account.My_Field__c);
   }
}
1

You are missing a WHERE filter for the records in the trigger context

trigger myTrigger on Contact(after update) {
   for(Contact cont : [SELECT id, Account.My_Field__c 
                        FROM Contact
                        WHERE Id IN :Trigger.newMap.keySet()]) {
       // this always returns null
       system.debug('account.my_field__c value: ' + cont.Account.My_Field__c);
   }
}
3
  • i did add that and it still did not retrieve a value for cont.Account.My_Field__c. I am still getting null returned. Any other ideas? – usernameabc Mar 23 at 1:35
  • Does the contact's Account have a value for that lookup field? Does the running user have FLS on that field? – cropredy Mar 23 at 2:22
  • double checked the FLS. I believe i may have found the core issue: when you query the way your code snippet is then I can fetch the field value as expected, but if I were to use Trigger.newMap.values() and say cont.Account.My_Field__c then I get an error. Through testing, I found the code snippet provided – usernameabc Mar 23 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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