You need to sign in to do that
Don't have an account?
Adam 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?
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?
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
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
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