1

Using v31.0 of the Salesforce REST API, when querying metadata for an object, if the object is of the type reference, it will have it's referenceTo field populated with the referenced table.

{
  "fields": [
    { 
      // ...
      "referenceTo": ["MyReferencedTable"],
      // ...
      "type": 'reference',
      // ...
    }]
}

Notice that the referenceTo field is array. To me this implies that the field could have more than one value. Is there a situation where I should expect a field that is a reference type to reference more than one table?

4

Some standard fields do contain multiple referenceTo objects. They are used for values like Attachment's ParentId, Task's and Event's WhoId and WhatId fields, etc, where the Id might reference any type of object that the field supports (e.g. all custom objects with "Allow Activities" enabled will appear in the WhatId referenceTo list). No custom lookup or master-detail relationship will contain multiple referenceTo objects. In the majority of cases, referenceTo will contain only one object, but you do need to be prepared to support multiple objects.

  • Lt's say there is more than one object in the referenceTo. Then how can we find out which object this id refers to? One way is to look at the first 3 characters of the ID (convention) but that does not work with custom objects. – Nader Ghanbari Dec 7 '15 at 9:10
  • @NaderHadjiGhanbari In Apex Code, you simply use record.MyLookupField.getSObjectType() to determine the type of object an ID refers to (of course, checking for nulls first). For example, you might see code that looks like this: if(leadRecord.OwnerId.getSObjectType() == Group.SObjectType) { /* The owner is a queue */ } else { /* The owner is a user */ }. – sfdcfox Dec 7 '15 at 16:56
  • Thanks @sfdcfox, I should have mentioned that this is concerning the REST API. – Nader Ghanbari Dec 11 '15 at 4:48

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.