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
withoutmewithoutme 

Limiting SOQL results

If List<Id> cid is a list of contact Id, I want to get a list of tasks associated with these contacts, but only 1 per contact Id.

Doing

[select t.Status, t.Type, t.WhoId from Task t where (t.WhoId in :cid and (t.type ='Call Completed - With Relevant Contact')) Limit 1] 

returns a total of only 1 task record. I want it to return one for each contact. Is this possible? Thank you 

Best Answer chosen by Admin (Salesforce Developers) 
Anand@SAASAnand@SAAS

You should do something like this:

 

for(Contact ct: [select Id,Name,{select Contact.Tasks from Task limit 1) from contact where Contact.MailingPostalCode='07666']{ Task[] tskList = ct.Tasks; ...}

 

 

 

All Answers

Anand@SAASAnand@SAAS

Try:

 

SELECT Contact.Name, (SELECT Task.Subject,Task.Id FROM Contact.Tasks limit 1) FROM Contact

 

 

 

withoutmewithoutme

Does not work. 

1. It returns a list of contacts. Not a list of task. ANd it returns the entire list of contacts.

2. So, if the relevant data is in the su-query, how do I drill down to it?

 

Anand@SAASAnand@SAAS

You should do something like this:

 

for(Contact ct: [select Id,Name,{select Contact.Tasks from Task limit 1) from contact where Contact.MailingPostalCode='07666']{ Task[] tskList = ct.Tasks; ...}

 

 

 

This was selected as the best answer