2

I have a Salesforce ID given client side. I'd like to retrieve the object's information, but I don't know what type it will be. How can I query the REST API for the object's type given the ID?

2
  • 1
    n.b. the first three chars of the ID are the object's keyprefix (ascertainable with a describe call)
    – cropredy
    Commented Oct 5, 2015 at 19:20
  • If you want to make an answer with a sample describe call, I'd be happy to accept that as the answer. Please account for 15-digit vs 18-digit IDs.
    – ricksmt
    Commented Oct 5, 2015 at 19:35

1 Answer 1

4

You can use the Describe Global REST call to get each object's keyprefix. A SFDC SObject ID conforms to a particular convention in that the first three characters match the key prefix

Describe Global call

https://na1.salesforce.com/services/data/v26.0/sobjects/

where na1.salesforce.com is just an example, your URL will vary. The example is taken from the doc here.

The result of the Describe Global (the get response body) is also given in the example from the doc here but for explication purposes, I shall replicate it:

{ 
  "encoding" : "UTF-8", 
  "maxBatchSize" : 200, 
  "sobjects" : [ { 
    "name" : "Account", 
    "label" : "Account", 
    "keyPrefix" : "001", 
    "labelPlural" : "Accounts", 
    "custom" : false, 
    "layoutable" : true, 
    "activateable" : false, 
    "urls" : { 
      "sobject" : "/services/data/v26.0/sobjects/Account", 
      "describe" : "/services/data/v26.0/sobjects/Account/describe", 
      "rowTemplate" : "/services/data/v26.0/sobjects/Account/{ID}" 
    }, 
    "searchable" : true, 
    "updateable" : true, 
    "createable" : true, 
    "deprecatedAndHidden" : false, 
    "customSetting" : false, 
    "deletable" : true, 
    "feedEnabled" : true, 
    "mergeable" : true, 
    "queryable" : true, 
    "replicateable" : true, 
    "retrieveable" : true, 
    "undeletable" : true, 
    "triggerable" : true 
  }, 
  ...
  ]
}

hence your client code does a Describe Global call, deserializes the JSON into a convenient data structure that uses keyPrefix as the key and (sobject) name as the value. Then, given the first three characters of the SFDC id in question, ascertain the Sobject name.

You must log in to answer this question.

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