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?

  • 1
    n.b. the first three chars of the ID are the object's keyprefix (ascertainable with a describe call) – cropredy Oct 5 '15 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 Oct 5 '15 at 19:35
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.

| improve this answer | |

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.