+ Start a Discussion
GrantMGrantM 

Displaying Task's 'WhatId' to the User

Hi

I am currently pulling down a Users tasks using the API.
I would like to show the user details about what this task belongs to.

Now the Task contains a field called 'WhatId' which is the Id of that which the task is related to.
Now i would like to query information from that object with the 'WhatId' but i don't know which object to query.

Salesforce has a great machanism where the salesforce url followed by an Id will bring up any object type.
How is this done and can i do this through the Api?
From a given Id, determine which object this Id belongs to

Thanks and Kind Regards

GrantM
DemandToolsDemandTools
Grant;

When you do a DescribeSObject (or DescribeSObjects) the DescribSObjectResult contains a property (KeyPrefix) that holds the first 3 characters of the key that relate to that object.

Compare those 3 characters to the first 3 in your ID and you'll have your object type.

GlennW
GrantMGrantM
Thank Glenn!

I thought it would have something to do with the DescribeSObject
GrantMGrantM
Ok, i've put this into practice, and its working fine.
The only limitation i'm finding is I have to specify a list of objects before hand.
So:

sforce.DescribeSObjectResult[] dsrs = binding.describeSObjects(new string[3]{"Contact", "Opportunity", "Case"});

So now i'm only expecting either a Contact, Opportunity or Case.
What if i want to query all objects, how can i do this?

I tried to Use the sforceservice.describeGlobal().Types to get list of all object type, then combining this with the statement above.
But this ended up taking too long.

So is there another way to do it? or is this the only way

Thanks Again in Advance
SuperfellSuperfell
the describe on WhatId for Task will list all the possible object types that it can refer to (see the referenceTo array). You can use this to decide what additional objects you need to describe.
GrantMGrantM
Thanks Simon

I can't find anything on "referenceTo array",
Please can you post some code or a link for me.

Thanks
SuperfellSuperfell
see the docs on the Field structure

http://www.sforce.com/us/docs/sforce60/sforce_API_calls_describeSObjects_Field.html#wp1610027
GrantMGrantM
Simon, I appreciate your help, but i'm still not getting anywhere.

So, i have a Task with a WhatId field.
Now i want to look this up and determine what object it belongs to.

Now from what you telling me, sforce.DescribeSObjectResult has a "referenceto" proprety that can determine which objects this WhatId belongs to.

I looked at the DescribeSObjectResult and saw it has a property called "fields" which is an array.
Each field has a "referenceto" property as well.
But how do i use this?

To get the DescribeSObjectResult i have to supply a list of objects
sforce.DescribeSObjectResult dsr = binding.DescribeSObjects(string[] ObjectTypes);

So i don't understand when and how i use the WhatId with the DescribeSObjectResult.

Please Help - code sample would be ideal
SuperfellSuperfell
1. call describeSObject on Task
2. walk through the fields collection looking for the whatId field
3. get the set of objects the whatId can reference by getting the referenceTo array on that field.
4. use this array to call describeSObjects
5. drink tea :)
GrantMGrantM
tea .... TEA ... YOU MEAN COFFEE!!

seriously, thanks, this looks like a easy to follow explanation.
i'll give it a try, and you know i'll post back if i'm stuck :)