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 

SQL join problem + select statement problem

I was trying to do the join statment (on task,contact) , but it is not working, do you know how to fix it?
SELECT   Owner.Name, Task.WhoId FROM Task, contact where task.id = contact.id
Sorry I am new to SOSQL, does SOSQL let us do  join statement like  ... from x,y where x.id = y.id?

Also i can not do select statement from nested select like : select x  from (select x, y from z) as table1, does sosql allow this kind of query?

Thank you!
HARSHIL U PARIKHHARSHIL U PARIKH
Salesforce does allows query inside the query.
Here are few ways you can solve the puzzle,
Let's say you have a list of all contact Ids such as
 
List<Id> contactIds = New List<Id>();

Now there are two ways to do the above operation.
1) You can write a query on TASK Object and get the results
2) You can write a query on Contact Object and gets the results

1)
List<Task> allTasks = [Select Id, Owner.Name, WhoId FROM Task WHERE WhoId = : ContactIds];
This query will give you all the tasks who has their WhoId in ContactIds list.

2)
List<Contact> contactsWithTasks = [Select Id, FirstName, LastName, (Select Id, Owner.Name, WhoId FROM Tasks) FROM Contact WHERE Id = : contactIds];
This query would give you all the contact with their associated attached tasks infomation.

In addition, at a time one SOQL (Salesforce Object Query Language) can fetch up to 50,000 records.

Hope this helps and Mark it best answer if it help you solve the query
Raj VakatiRaj Vakati
SELECT   Owner.Name, Task.WhoId FROM Task where WhoId IN (Select Id from Contact)
IT Admin 179IT Admin 179
It seems like apex code, I do not have experience doing it, do you know the steps that run this code?

"
List<Id> contactIds = New List<Id>();
List<Task> allTasks = [Select Id, Owner.Name, WhoId FROM Task WHERE WhoId = : ContactIds];
List<Contact> contactsWithTasks = [Select Id, FirstName, LastName, (Select Id, Owner.Name, WhoId FROM Tasks) FROM Contact WHERE Id = : contactIds];

"
HARSHIL U PARIKHHARSHIL U PARIKH
Yes! I can followup on how those steps gets run but they need to have somesort of boundry around it such as class or trigger or an Anonymous apex code.

Can you please tell what are your requirements though? Since above code has contactIds as null and it would not run for now.

But this is what the code tells you.
List<Id> contactIds = New List<Id>();
List<Task> allTasks = [Select Id, Owner.Name, WhoId FROM Task WHERE WhoId = : ContactIds];
List<Contact> contactsWithTasks = [Select Id, FirstName, LastName, (Select Id, Owner.Name, WhoId FROM Tasks) FROM Contact WHERE Id = : contactIds];

First you have a list of Ids which contains bunch of Ids under it and these ids can be a Contact record ids or Opportunity record IDs or Account records Ids etc..But in our case let's imagine that they are contact record ids.
Let's say you have 50.000 contacts inside your Salesforce and this list has around let's say 50 of them.

Now, in second List of Task what is doing is it is grabbing each records from Task object but at the same time it is making sure that they are all related to the at least one record of contact whoes Id is in contactId list.

On 3rd list what it does is it is getting all contact records with their associated Tasks information but at the same time it is making sure that only those contacts gets pulled whoes Id is in contactId list.

The reason we define contactId list is just to make sure that we don't grab un wanted records inside the Lists but rather only those whoes Ids matches with the contactIds.

Hope this helps!