1

Can someone please explain how to reference fields from parent thru custom lookup?

To clarify, I created the example below, where the compiler hates line 7 (but there is no issue on line 3).

Line: 7, Column: 11 Variable does not exist: Account

1    for (Contact c : [SELECT Id from Contact WHERE CreatedDate = TODAY]){
2        if (c.Account.Count__c != null)
3            c.Account.Count__c = 50;
4    }
5    
6    for (Employee__c e : [SELECT Id from Employee__c WHERE CreatedDate = TODAY]){
7        if (e.Account.Count__c != null)
8            e.Account.Count__c = 100;
9    }

When I change line 7 to the lookup name 'Account__c', the compiler also hates that:

7         if (e.Account__c.Count__c != null)

Line: 7, Column: 11 A non foreign key field cannot be referenced in a path expression: Account__c

Question 1: Why on Contact there is possibility to use "Account" reference? The lookup to Account is named "AccountId", so I don't get it. Is it because there is some standard relationship or a different reason?

Question 2: How to work with such scenarios on objects with custom lookups/objects?

Question 3: If there is more to it, can you please refer me to some articles?

2 Answers 2

1

You need to query for related data

// As a general rule, just always use braces
// Consistency is king, and it's really easy to gloss over the fact that only the
//   next statement is part of the implicit block (also, explicit > implicit)

// Need to query for related data
// Uses the same dot-notation as outside of the query
for (Contact c : [SELECT Id, Account.Count__c from Contact WHERE CreatedDate = TODAY]) {
    // Using dot notation to access related data is correct
    // Standard relationship fields (<something>Id) need to have the "Id" bit dropped
    //   when traversing up the relationship hierarchy (from the "many" side to the
    //   "one" side)
    if (c.Account.Count__c != null) {
        c.Account.Count__c = 50;
    }
}
// Again, you need to query for the related data
// Each individual related field needs to be queried, using the relationship field
//   as a prefix
for (Employee__c e : [SELECT Id, Account__r.Count__c from Employee__c WHERE CreatedDate = TODAY]) {
    // Custom relationship fields (<field name>__c) need the __c changed to __r
    if (e.Account__r.Count__c != null) {
        e.Account__r.Count__c = 100;
    }

}
1
  • Thanks a lot for your explanation, Derek! Especially the part "somethingId need to have the Id bit dropped" helped me a lot to generalize. I have additional question to "Need to query for related data" because my code actually work without querying "Account.Count__c" - is there some general rule of when I need to query it and when it is not necessary and why?
    – Mywen
    Commented Aug 20 at 10:09
1

Question 1: Why on Contact there is possibility to use "Account" reference? The lookup to Account is named "AccountId", so I don't get it. Is it because there is some standard relationship or a different reason?

Most standard master-detail and lookup fields have a "relationship name". This is how you reference a related object. As another example, CreatedById can reference the record creator's name with CreatedBy.Name.

Question 2: How to work with such scenarios on objects with custom lookups/objects?

Custom relationships use __r. For example, if Employee__c has a field Account__c, the relationship will be Account__r.

Question 3: If there is more to it, can you please refer me to some articles?

Understanding Relationship Names, Custom Objects, and Custom Fields explains custom relationships in more detail.

1
  • Thanks a lot, all clear!
    – Mywen
    Commented Aug 20 at 10:03

You must log in to answer this question.

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