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 contact -> task

1.   I was trying to do the soql code for 

select contact.name from task
or select contact__r.name from  Task__C

both do not work

contact and task are having relatinoship of parent to child
-----------------------

2.   I am trying to code soql in order to get the information that:

 get name for each task : (task1. name= John, Smith   ,  Task2.name = John, Smith)   --Name in the Task can appear more than once since there are many task of same person


may I get help from You?
Thanks

Tim
HARSHIL U PARIKHHARSHIL U PARIKH
It's because there is no NAME field on contact though that's why and Task is a special object in Salesforce and you can not directly query on all fields of parent object.

I would query like this,
List<Contact> gettingContactRecords = [Select Id, FirstName, LastName, (Select Id, WhoId FROM Tasks) FROM Contact];
Above query will fetch all the contacts in the database (I mean at least 50,000) but however, if you want to fetch only specifics contacts then you can define List of Ids and then fetch accordingly. How?

List<Id> ConIds = New List<Id>();
Imagine this list has about 20 contact Ids. (The specific ones that you needed task records for)

Now we can just simply change our query by little by adding WHERE cluse.
List<Contact> gettingContactRecords = [Select Id, FirstName, LastName, (Select Id, WhoId FROM Tasks) FROM Contact WHERE Id =: ConIds];
This way our list named "gettingContactRecords" would only fetch those 20 contacts with their task information.

Hope this helps and if it solves the query then please mark it solved!

 
Asif Ali MAsif Ali M
Hi Tim,

Use the Below SOQL to get Contact Tasks. Use the parent relationship 'Who' to filter on parent fields
SELECT Id, Who.Name FROM Task where Who.Type='Contact'