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
IT Admin 179IT Admin 179 

soql task

i am trying to do soql on task table:


according to the task from salesforce, according to the picture i posted, how can I retrieve 

attribute "name" and "related to" >?

 

thank you!User-added image

Best Answer chosen by IT Admin 179
HARSHIL U PARIKHHARSHIL U PARIKH
Hi IT Admin 179,

would you mind marking the question solved (Best Answer) if it helps you out or solves the query.. Since it will helps other users who falls under similar situation.
Thank You much sir!

All Answers

venkat-Dvenkat-D
Query for WhoID and WhatID from task .

https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_erd_activities.htm
 
HARSHIL U PARIKHHARSHIL U PARIKH
Hi IT Admin,

On task obejct field named "Relate To" has backend API name as WhatId. And field named "Name" has API name as WhoId.

So, let's say you want to retrive all the tasks inside the database with their RelateTo and Name field info then I would invite you to take a look on the below query.
 
List<Task> allTasks = [Select Id, WhoId, WhatId FROM Task LIMIT 50000];
Also keep in mind that WhoId is always from either LEAD or CONTACT. But for all others such as Account, Opportunity, Case etc.. its WhatId.

Now, if you get a Id then how would would you kow which object is it from?

There is a function in salesforce with allows us to find this out. Somethig along the following lines..

 
For(Task tsk : Trigger.New)
{
    String str = tsk.WhatId.getSobjectType();
    String abc = tsk.WhoId.getSobjectType();
    
    System.debug('The Object Name Is: ' + str);    
     System.debug('The Object Name Is: ' + abc ); 
}

Hope this helps and if it answers your question then please mark it as best answer!
IT Admin 179IT Admin 179
in the for loop there is an error that:

Line: 8, Column: 12
Illegal assignment from Schema.SObjectType to String

please help!
HARSHIL U PARIKHHARSHIL U PARIKH
Hi IT Admin,

Good catch! Here we go. I have tried this one out in my org and I am getting currect out put.
 
Trigger DisplayName On Task(After Insert, After Update){
     
     If(Trigger.IsInsert || Trigger.IsUpdate){
         For(Task tsk : Trigger.New)
         {
             If(tsk.WhoId != Null)
             {
                 
                 System.debug('Associated Object Is: ' + tsk.WhoId.getSObjectType());
             }
             
             If(tsk.WhatId != Null)
             {
                 
                 System.debug('Associated Object Is: ' + tsk.WhatId.getSObjectType());
             }
         }
     }   
}

First I have created a task under the Lead object and got the below output.
User-added image

Then I have created a New task on an Opportunity obj and here got the below output.
User-added image

Hope it helps and if it solves the question then please mark it as best answer!
HARSHIL U PARIKHHARSHIL U PARIKH
Hi IT Admin 179,

would you mind marking the question solved (Best Answer) if it helps you out or solves the query.. Since it will helps other users who falls under similar situation.
Thank You much sir!
This was selected as the best answer