I have an object id that is a "parent id" of a specific Attachment object (e.g., b3KD0000119wDTlMAN). I'd like to understand what type this object belongs to, to query for it's details. It can also be a Custom Object. Currently I only work through REST API. Can this be done using REST API?
3 Answers
You can query the parent's type directly in the SOQL:
SELECT ParentId, Parent.Type FROM Attachment WHERE Id = ...
You'll get a response like this:
{
"done": true,
"records": [
{
"attributes": {
"type": "Attachment",
"url": "/services/data/v33.0/sobjects/Attachment/..."
},
"ParentId": "...",
"Parent": {
"attributes": {
"type": "Name",
"url": "/services/data/v33.0/sobjects/Opportunity/..."
},
"Type": "Opportunity"
}
}
],
"totalSize": 1
}
-
Thanks a lot sfdcfox. This is a much better solution indeed. Commented Mar 1, 2015 at 10:00
-
You can get object type by Id using dynamic apex
Schema.SObjectType token = objIds[0].getSObjectType();
where objIds is a list of Ids.
Read more on this link.
You have to add all these methods (as per your requirement) in your REST service provider class.
As far as i know there is no such function available but you can create your own library to find object using first 3 letter of id here is the list for some standard salesforce object.https://help.salesforce.com/apex/HTViewSolution?id=000005995&language=en_US
-
1