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
Janardhana ReddyJanardhana Reddy 

Soql not return contact related tasks in batch apex

i was written soql query in my batch class
select id,Status,Stage,WhoId from Task where Whoid =: ContactId and Status =: 'Completed'
 the query not getting tasks for contact
please help me
Best Answer chosen by Janardhana Reddy
srlawr uksrlawr uk
hmm. So to clarify,

You have two tasks in your org, each related to a different contect (but otherwise, the same?).

When you query in the developer console query editor, you can get one to come back, but not the other?


If  you are working in test class/method, which I suspect you may be, you are definitly remembering that you cannot access org data from the test methods by default. Another thing I would also look into is when tasks are marked as completed, they are moved into the "activity history" phase of their lifecycle, which might actually have some affect on what you are trying to do? (not sure on that one).

All Answers

Virendra ChouhanVirendra Chouhan
Hi Janardhana,

I think you have to remove " : " before you write 'Completed' in soql query.

Janardhana ReddyJanardhana Reddy
Hi Virendra,
Same result, after modification i run the query in console for particular contact, it is giving same result No rows.
Query i Used: select Id,Subject,Status from Task where WhoId = 'xxxxxxx'
but this is working for some contacts
Virendra ChouhanVirendra Chouhan
Hi Janardhana,

If your contact haven't any Task then this query gives us No row.
so please put the id of particular Contact wich have any open Task.

Regards
Virendra
srlawr uksrlawr uk
This sounds daft but, are you definitly sure the contacts you are examining have Tasks associated with them? If the query isn't generating an error (which it sounds like it is not) and it working for "some contacts" then one can only assume that it is the "data" that is "wrong" and not the query...

Remember Tasks have a WhoId and a WhatId - used for different purposes, as you can read briefly here,

https://developer.salesforce.com/forums/ForumsMain?id=906F00000008wZlIAI

and in the docs here

https://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_task.htm

Janardhana ReddyJanardhana Reddy
Hi Srlawr,
Contact what i am using for testing is having 2 Tasks,  and those having WhoId related to Contact. i am not getting what is going on around the Task.

Janardhana ReddyJanardhana Reddy
Hi Virendra,
Is there any permissions needed for Task object to get the records
srlawr uksrlawr uk
hmm. So to clarify,

You have two tasks in your org, each related to a different contect (but otherwise, the same?).

When you query in the developer console query editor, you can get one to come back, but not the other?


If  you are working in test class/method, which I suspect you may be, you are definitly remembering that you cannot access org data from the test methods by default. Another thing I would also look into is when tasks are marked as completed, they are moved into the "activity history" phase of their lifecycle, which might actually have some affect on what you are trying to do? (not sure on that one).
This was selected as the best answer