function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Adam MahameedAdam Mahameed 

How to get sobject by ID only using REST API

Hello,

I would like to retrieve a record using its ID only(not knowing object type) using REST API.
All REST API documentation mentions the method where sobject type is known and added 
Ex:  /services/data/v51.0/sobjects/Customer__x/x01D0000000002RIAQ

I would like to get the object, or even it's object type using its ID only
As this works great without issues via browser "https://cs22.lightning.force.com/x01D0000000002RIAQ"

Is there anyway to achieve it via REST API? if not what could be done to get the sobject type by ID?
Best Answer chosen by Adam Mahameed
Omar Rajab 94Omar Rajab 94

Hi Adam,

Firstly you need to make Describe Global call e.g. https://na1.salesforce.com/services/data/v26.0/sobjects/

The Response is a list of all objects informations you have in the Salesforce Org
{
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}" }
    }, 


    "name" : "Contact", 
    "label" : "Contact", 
    "keyPrefix" : "003", 
    "labelPlural" : "Contacts", 
    "custom" : false, 
    "layoutable" : true, 
    "activateable" : false, 
    "urls" : { 
      "sobject" : "/services/data/v26.0/sobjects/Contact", 
      "describe" : "/services/data/v26.0/sobjects/Contact/describe", 
      "rowTemplate" : "/services/data/v26.0/sobjects/Contact/{ID}" }
]

... 
...
}

As you see the response contains the list „sobjects“, which includes the fields keyPrefix and label. So what you shoud to do is, to create a map, the key is the keyPrefix and the value is the label.
the map look like this
001 => Account
003 => Contact 
xxx => customObject__c 

During the first three chracters in Salesforce Id, you can know the object type.

Let suppose the Salesforce Id you need to retrieve is 0011U00000TFV7MQAX. The first three characters are „001“. Now it is easy to know the object type, because we know, that the map we created from ghe json above contains: 
KeyPrefix: 001 
Label: Accounts 

So the obejct type for this id is Account!

Regards,
Omar

All Answers

Omar Rajab 94Omar Rajab 94

Hi Adam,

Your Question is solved here:
https://salesforce.stackexchange.com/questions/94750/query-rest-api-for-type-given-id
 

please let me know if this solved your question!
 

Regards,
Omar

Adam MahameedAdam Mahameed
Hello Omar, Thank you for your reply. I believe this doesn't answer my question, as this gives me all sobjects types available. I need the sobject type for this specific ID. If I misunderstood, could you please provide an example? Thanks
Omar Rajab 94Omar Rajab 94

Hi Adam,

Firstly you need to make Describe Global call e.g. https://na1.salesforce.com/services/data/v26.0/sobjects/

The Response is a list of all objects informations you have in the Salesforce Org
{
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}" }
    }, 


    "name" : "Contact", 
    "label" : "Contact", 
    "keyPrefix" : "003", 
    "labelPlural" : "Contacts", 
    "custom" : false, 
    "layoutable" : true, 
    "activateable" : false, 
    "urls" : { 
      "sobject" : "/services/data/v26.0/sobjects/Contact", 
      "describe" : "/services/data/v26.0/sobjects/Contact/describe", 
      "rowTemplate" : "/services/data/v26.0/sobjects/Contact/{ID}" }
]

... 
...
}

As you see the response contains the list „sobjects“, which includes the fields keyPrefix and label. So what you shoud to do is, to create a map, the key is the keyPrefix and the value is the label.
the map look like this
001 => Account
003 => Contact 
xxx => customObject__c 

During the first three chracters in Salesforce Id, you can know the object type.

Let suppose the Salesforce Id you need to retrieve is 0011U00000TFV7MQAX. The first three characters are „001“. Now it is easy to know the object type, because we know, that the map we created from ghe json above contains: 
KeyPrefix: 001 
Label: Accounts 

So the obejct type for this id is Account!

Regards,
Omar
This was selected as the best answer
Adam MahameedAdam Mahameed
Thanks Omar! This answers my question, well described thank you