Hopefully this is a simple oversight on my part, but I can not see why when I loop through my Trigger.new, one of my field values is not populating.

The field (contact's-> account's-> Web_Id_NatFund__c field) 100% has a value, I can query for it and the value shows. But when I run this:

for(Contact c : Trigger.New) {
    if (c.Account.Web_Id_NatFund__c != NULL) {....

c.Account.Web_Id_NatFund__c comes in as a null.

Is there an explanation for this?

My only thought is when I system.debug the Trigger.new or the values of the Contact, the actual field name is AccountId. Is this the possible issue?

Trigger New:: (Contact:{Id=0030t000001rJ65AAE, IsDeleted=false, MasterRecordId=null, AccountId=0010t000002mBnFAAU,....

When I mess around with c.AccountId.Web_Id_NatFund__c, it rejects the AccountId field saying Error:A non foreign key field cannot be referenced in a path expression: AccountId. I am able to pull out other custom and standard field values of the Contact.

Shouldn't it just understand what c.Account.AnyFieldNameCustomOrStandard is?

  • 1
    Related objects are not populated for the object in a trigger, so you'll need to explicitly retrieve the accounts for your contacts. Nov 28 '17 at 3:55
  • ahh thank you so much I thought I was going crazy. That is interesting! For anyone reading, I will query for the field value and ensure there is not a null value before entering the initial conditional. If you want to post that as the answer I will mark correct.
    – Olivia
    Nov 28 '17 at 4:18

Related objects are not populated for the object in a trigger, so you'll need to explicitly retrieve the accounts for your contacts. You need to loop through your contacts to collect the account IDs, then retrieve the accounts into a Map<Id, Account> so you can reference them using Contact.AccountId

  • Another quick-and-dirty test: If you need to use more than one dot/period/full-stop to access the field (i.e. c.Account.Web_Id_NatFund__c, which uses 2 periods), then it is not available in trigger context variables like Trigger.new, and you need to query to get that data.
    – Derek F
    Nov 28 '17 at 13:41
  • @DerekF how would I go about querying the data? I am unable to query inside the loop but I would need each Contact's Account Id...do you think the best idea would be to save the Contact's Id and the AccountId into a map and then look at the map on the outside of the loop?
    – Olivia
    Nov 28 '17 at 17:03
  • @Olivia that is correct, I updated my answer to include that. Nov 28 '17 at 17:45
  • @Olivia I really think you're trying to over-complicate matters here. It really is just a basic query. Instead of using Trigger.new for your loop, use a soql-for-loop. Use dot-notation in the query to grab the fields from Account that you want. Use Trigger.new to control the Contact records that you query for.
    – Derek F
    Nov 28 '17 at 18:04
  • @DerekF you are right, I am way over complicating it. I used: for(Contact c : [SELECT Id,pi__url__c, Account.Web_Id_NatFund__c FROM Contact WHERE Id IN :Trigger.new]) and was able to retrieve all the data necessary.
    – Olivia
    Nov 28 '17 at 19:24

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.