Skip to main content
add not about standard lookup fields
Source Link
Jason Clark
  • 12.2k
  • 7
  • 64
  • 123

Without sample code, it's hard to be certain, but I think you are saying you have something like this:

//You have a custom object, Widget__c, with a lookup to account
Widget__c widget = [select id, name, Account__c from Widget where id = :someId];

List<Account> accounts = new List<Account>();
accounts.add(widget.account__c);  // <-- error here, because account__c is an id

Instead of account__c, use account__r to refer to the object:

account.add(widget.account__r); // this works.

Note however that the account in the list will only have loaded the field values loaded in the original query. So if you will need the account.name (for example), add account__r.name to the query.

Also - the syntax is a little different for standard lookup fields. For example, if you were getting the Account from a Contact object, accountid refers to the id, but account refers to the object. This is consistent for all standard lookup fields that end with "id".

Without sample code, it's hard to be certain, but I think you are saying you have something like this:

//You have a custom object, Widget__c, with a lookup to account
Widget__c widget = [select id, name, Account__c from Widget where id = :someId];

List<Account> accounts = new List<Account>();
accounts.add(widget.account__c);  // <-- error here, because account__c is an id

Instead of account__c, use account__r to refer to the object:

account.add(widget.account__r); // this works.

Note however that the account in the list will only have loaded the field values loaded in the original query. So if you will need the account.name (for example), add account__r.name to the query.

Without sample code, it's hard to be certain, but I think you are saying you have something like this:

//You have a custom object, Widget__c, with a lookup to account
Widget__c widget = [select id, name, Account__c from Widget where id = :someId];

List<Account> accounts = new List<Account>();
accounts.add(widget.account__c);  // <-- error here, because account__c is an id

Instead of account__c, use account__r to refer to the object:

account.add(widget.account__r); // this works.

Note however that the account in the list will only have loaded the field values loaded in the original query. So if you will need the account.name (for example), add account__r.name to the query.

Also - the syntax is a little different for standard lookup fields. For example, if you were getting the Account from a Contact object, accountid refers to the id, but account refers to the object. This is consistent for all standard lookup fields that end with "id".

Source Link
Jason Clark
  • 12.2k
  • 7
  • 64
  • 123

Without sample code, it's hard to be certain, but I think you are saying you have something like this:

//You have a custom object, Widget__c, with a lookup to account
Widget__c widget = [select id, name, Account__c from Widget where id = :someId];

List<Account> accounts = new List<Account>();
accounts.add(widget.account__c);  // <-- error here, because account__c is an id

Instead of account__c, use account__r to refer to the object:

account.add(widget.account__r); // this works.

Note however that the account in the list will only have loaded the field values loaded in the original query. So if you will need the account.name (for example), add account__r.name to the query.