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
snippets@sfsnippets@sf 

How can i get details of related to ie the WhatId of Task Object in SOQL.

Hi,

 

How can i get details of related to ie the WhatId of Task Object in SOQL.

CustomObject has a field as Name

Example : Task t1 is related to CustomObject

SOQLQuery could be [SELECT id,whatid from Task];

This will result me in taskid and the customObject record id but what if i need that complete record of customObject.

 

 

 

Appriciate any inputs on this in advance
Thanks.

Vinit_KumarVinit_Kumar

If I am correct Task is child and custom object is your parent.Then,the query should be like 

 

select id,whatId,what.name,what.<custom object field API name>/ from Task

 

here,what.name > Name of custom object record.

         what.<custom object field API name>  > You can use any field of custom object to query here.

 

 

LakshmanLakshman

You could do something like below:

 

Schema.SObjectType result;
map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
String prefix;
map<String, String> prefixMap = new map<String, String>();
for (Schema.SObjectType ot : gd.values())
{
   prefix = ot.getDescribe().getKeyPrefix();
   if (prefix != null)
   {
    prefixMap.put(prefix, ot.getDescribe().getLocalName());
   }
}
String sub;
String apiName;
for(Task t : [Select Id, WhatId from Task]
{
   sub = String.valueOf(t.WhatId).substring(0, 3);
   if (sub != null)
   { 
    apiName = prefixMap.get(sub);//This the object label
    if (apiName != null && apiName = 'Custom_Object__c')
    {
     //Do the necessary operations like retrieving fields or dynamic soql query string stored in map of task id to query string
    }
   }
}

 Let me know if that helps.

 

Regards,

Lakshman

vishal@forcevishal@force

Why not use this new feature - SOQL Polymorphism

 

This should make your job a lot easier!

AnshulVermaAnshulVerma

SOQL Polymorphism is the key to go ahead in terms of accessing specific fields or filtering records based on related types.

 

For a sneak peek on polymorphic keys you can visit http://mightycoder.blogspot.in/2013/06/sneak-peak-salesforce-polymorphic-keys.html