+ Start a Discussion
lakslaks 

Retrieving list of Tasks for a Case

Hi,

 

Can anybody pls tell me how to retrieve list of Tasks for a particular Case using Apex coding.

Best Answer chosen by Admin (Salesforce Developers) 
kyle.tkyle.t

I assume you are planning on doing this in a trigger?

 

You need to query the Task table for the ID of the case.  Since tasks can relate to multiple objects, you need to check the WhatId.

 

The query should be something like this:

 

List<Case> caseList = [select id, name, subject from Task where WhatId IN :trigger.New];

All Answers

kyle.tkyle.t

I assume you are planning on doing this in a trigger?

 

You need to query the Task table for the ID of the case.  Since tasks can relate to multiple objects, you need to check the WhatId.

 

The query should be something like this:

 

List<Case> caseList = [select id, name, subject from Task where WhatId IN :trigger.New];

This was selected as the best answer
lakslaks

Thanks a lot for your reply kyle :-)

 

I was not going to use it in a trigger.

 

This is what i tried, and it worked fine :

 

 

Id CaseID = Caseobj.Id;  //Had already fetched the case object in the previous step


List<Task> TaskList = [Select Id, Subject, ActivityDate, Status, Priority, OwnerId, Description, IsDeleted, IsClosed, CreatedDate, LastModifiedDate, LastModifiedById From Task where WhatId = :CaseID];

Andreas ChristidesAndreas Christides
Hi laks, 

I'm quite new to Apex, could you tell me where you put the "Id CaseID = Caseobj.Id;" or how to fetch the case object ? 

That'd help me greatly !

Regards,