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
Milan VadhiaMilan Vadhia 

how to get list of objects held in related to field of task sobject

Hello, I have a query and tried searching a solution over google and salesforce developers forums too. But I am not able to get the solution.

We all are talking how to manipulate whatId of Task SObject. but no one explained about the dropdown list shown in related To field (What field).

What should I do to retrieve all those objects which are displayed in related to field pick list in task object.

I have added sceen shots to explain what I want to get.

Please do help.

Thank you.

User-added image


User-added image

I want all these objects to be retrieved.
Best Answer chosen by Milan Vadhia
Milan VadhiaMilan Vadhia
Use gobalDriscribe methods to achieve this.

Code to retrieve​ 'Related To' (What) field search scope Objects.
Set<Schema.SObjectType> objActivityAllowed = new Set<Schema.SObjectType>();
String objectNames = '';
for (Schema.SObjectType objectType : Task.WhatId.getDescribe().getReferenceTo()) {
   objActivityAllowed.add(objectType);
   objectNames += '\n' + objectType;
}

system.debug(objectNames);

Code to retrieve​ Name (Who) field search scope Objects.
Set<Schema.SObjectType> objActivityAllowed = new Set<Schema.SObjectType>();
String objectNames = '';
for (Schema.SObjectType objectType : Task.WhatId.getDescribe().getReferenceTo()) {
   objActivityAllowed.add(objectType);
   objectNames += '\n' + objectType;
}

system.debug(objectNames);