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
mavsmavs 

SOQL Count

HI

 

I have a String List which holds ContactIds

List<String> contactIds = new List<String>();

 

I need to check if there are any Tasks exits for each ContactId in the List. Can someone please give me a clue on how to do this?

 

I am trying the following but, i get the error   Illegal assignment from Integer to LIST<Integer>

List<Integer> taskcount = new List<Integer>();

taskcount = [Select count() from Task where whoId IN :contactIds];

 

 

Please Advise

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Cory CowgillCory Cowgill

List<Contact> contacts = [Select Id, (Select Id from Contact.Tasks) from Contact where id in:contactids];
Map<Id,Integer> contactMapCount = new Map<Id,Integer>();
for(Contact aContact : contacts)
{
contactMapCount.put(aContact.Id,aContact.Tasks.siz

e());
system.debug(aContact.Tasks.size());
}

All Answers

Cory CowgillCory Cowgill

Integer taskcount = [Select count() from Task where whoId IN :contactIds];

mavsmavs

Hi -

 

I am looking the Task count for each contactId in the list, The query you suggested will not give this.

 

Can you have an alternate suggestion?

 

Thanks

Cory CowgillCory Cowgill

OK, I think I see what you want.

 

You want the Task count for each Contact, not the total count of tasks on all the contactIds.

 

This can do that.

 

List<Contact> contacts = [Select Id, (Select Id from Contact.Tasks) from Contact limit 100];
Map<Id,Integer> contactMapCount = new Map<Id,Integer>();
for(Contact aContact : contacts)
{
contactMapCount.put(aContact.Id,aContact.Tasks.size());
system.debug(aContact.Tasks.size());
}

 

Cory CowgillCory Cowgill

List<Contact> contacts = [Select Id, (Select Id from Contact.Tasks) from Contact where id in:contactids];
Map<Id,Integer> contactMapCount = new Map<Id,Integer>();
for(Contact aContact : contacts)
{
contactMapCount.put(aContact.Id,aContact.Tasks.siz

e());
system.debug(aContact.Tasks.size());
}

This was selected as the best answer
mavsmavs

That works.

 

Thank you so much.

shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com

Hi ,

    I wanna do the same thing, but with opportunity . I wanna get count()/opportunity of tasks where task name != XYZ . Can you please help me modify your code to accomadate this extra where clause?