5

For example, in the standard Salesforce Event object, I'd like to do the following with the WhatId field.

select what.name, what.type(?) from event where id = 'myid'

However, this will only work if the relationship object that I'm querying has a type column, which will not be the case. How can I know the "type" of relationship object I'm querying, without doing additional queries if possible.

Note: I'm using v31.0 of the REST API

2 Answers 2

2

Shouldn't your example query give you back what you need? In your sample SOQL, What.Type would be the API name of the object for the WhatId. For example, if you have a custom object called CustomObject__c and it's set as the WhatId on a task or event, What.Type would be 'CustomObject__c'. It's not looking for a field called 'Type' on the object CustomObject__c, it's telling you that the type of the WhatId is CustomObject__c.

Another way to get this information is to use the Id class's method of getSObjectType. This will also return the API name of the object (CustomObject__c, Opportunity, etc).

String objectType = String.valueOf(task.WhaId.getSObjectType());
1
  • This is what I had found as well. My error was assuming that I could call .Type on other fields other than polymorphic fields. Commented Jul 30, 2014 at 12:41
3

Not in a SOQL query, no.

One option might be to extract the whatID's keyprefix -- the first 3 characters of the whatID -- and compare it to the list of prefixes returned when you do a global object describe in that org.

You must log in to answer this question.

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