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
VaderVader 

Returning a List Derived from Another List

I am having trouble determining how to do this properly.    I have an Apex Class that has a list that is built based on a SOQL query into a custom object that contains Task IDs.  I want to use that list to iterate through the Task object to return the values below to another list object that is then referenced in a Visualforce data table by calling "{!getMyInformedTasks}". 

 

What is below just seems off in how it's constructed.  I am getting the error, "Save error: Variable does not exist: getMyInformedTasks"

 

 

        Id currentUserId = userinfo.getUserId();
	
	List<Task_Informed_Users__c> informedTaskList = [SELECT task_id__c FROM Task_Informed_Users__c WHERE informed_User__c =: currentUserId];
	
	public List<Task> getMyInformedTasks() {
		for(Task_Informed_Users__c tIU : informedTaskList) {
			for(Task t : tIU) {				
				getMyInformedTasks.add(t.Id);
				getMyInformedTasks.add(t.Subject);
				getMyInformedTasks.add(t.Description);
				getMyInformedTasks.add(t.Status);
				getMyInformedTasks.add(t.WhatId);
				getMyInformedTasks.add(t.What.Name);
				getMyInformedTasks.add(t.Owner.Name);
				getMyInformedTasks.add(t.OwnerId);
				getMyInformedTasks.add(t.CreatedBy.Name);
				getMyInformedTasks.add(t.CreatedById);
				getMyInformedTasks.add(t.ActivityDate);
				getMyInformedTasks.add(t.Task_Due_Date__C);							
			}
		}		
		return getMyInformedTasks;		
	}

 

Arun MKArun MK

Hi,

 

I believe you are trying to add the tasks assigned to the logged in user to MyInformedTasks list.

 

Find the code below:

 

Id currentUserId = userinfo.getUserId();

 

List<Task_Informed_Users__c> informedTaskList = [SELECT task_id__c FROM Task_Informed_Users__c WHERE informed_User__c =: currentUserId];

 

public List<Task> getMyInformedTasks() {

    Set<Id> setOfTaskIds = new Set<Id>();

 

    for(Task_Informed_Users__c tskInfUsr : informedTaskList){

        setOfTaskIds.add(tskInfUsr.task_id__c);

    }

    for(Task t : [Select Id, Subject, Description, Status, WhatId, What.Name, Owne.Name, OwnerId, CreatedBy.Name, CreatedById, ActivityDate, Task_Due_Date__C FROM Task WHERE Id in: setOfTaskIds]) {

        MyInformedTasks.add(t);

        }

    return MyInformedTasks;

}

 

Regards,

Arun.

 

Press Kudos if you are satisfied with the answer.

David SupuranDavid Supuran

Here is what it seems like you want to do, and this would be the most efficient way to do it:
** As a side note call {!MyInformedTasks} from the visualforce page mergefield, you might have been trying to use {!getMyInformedTasks} for example, which is wrong in this context.


public List<Task> getMyInformedTasks()
{
return [SELECT Subject,
Description,
Status,
What.Id,
What.Name,
Owner.Id,
Owner.Name,
CreatedBy.Id,
CreatedBy.Name,
ActivityDate,
Task_Due_Date__c
FROM Task
WHERE Id IN (Select Task_Id__c FROM Task_Informed_Users__c WHERE Informed_User__c = :currentUserId)];
}

VaderVader

Thanks Arun for your time!

 

I get the following error with the code snippet you listed: "Save error: Variable does not exist: MyInformedTasks"

 

I had this same issue earlier where for some reason it doesn't recognize the return list name.  Still not quite sure how to get past that.

VaderVader

Thanks David for helping out!  

 

The only issue with the code listed below is that the field Task_Id__c is a String field that contains the 15 char length ID that I am using to match ID's gathered from the url ID parameter when browsing through individual tasks.  I received an error in the code that stated:  "Save error: semi join sub selects can only query id fields, cannot use: 'Task_Id__c'

 

For whatever reason SFDC doens't have the ability to create a lookup field against the task or activity objects.  That's why I went with a string field as opposed to a lookup or ID field.  I am assuming this is the reason for the error when using the method you recommended.

VaderVader

I am still testing but I think I have it working now utilizing Arun's code.  Only modification(s) I made were I created a list object outside the for loop named MyInformedTasks and added an additional field that I needed in the SOQL.  Working (tenative) code below:

 

Id currentUserId = userinfo.getUserId();
 
	List<Task_Informed_Users__c> informedTaskList = [SELECT task_id__c FROM Task_Informed_Users__c WHERE informed_User__c =: currentUserId];
 	List<Task> MyInformedTasks = new List<Task>{};
 	
	public List<Task> getMyInformedTasks() {
    	Set<Id> setOfTaskIds = new Set<Id>();
 
    	for(Task_Informed_Users__c tskInfUsr : informedTaskList){
        	setOfTaskIds.add(tskInfUsr.task_id__c);
    	}
    	
    	for(Task t : [Select Id, Subject, Description, Status, WhatId, What.Name, Owner.Name, OwnerId, CreatedBy.Name, CreatedById, ActivityDate, Task_Due_Date__C, LastModifiedDate FROM Task WHERE Id in: setOfTaskIds]) {
        	MyInformedTasks.add(t);
        }
    return MyInformedTasks;
}