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
Sascha DeinertSascha Deinert 

Invalid bind expression type of TaskRelation for Id field of SObject Task

Hello,

I need to query all related task of the current contact. But If I run my code below I get always an error: Invalid bind expression type of TaskRelation for Id field of SObject Task

What is wrong? :-(
 
List<Taskrelation> RelatedTaskIds = [select taskId from taskrelation where relationid = :currentRecordId];                
for(Task a: [select Id, Subject, CreatedDate, Createdby.Name, Owner.Name, RecordType.Name, Status, Who.Name, Who.Id, What.Name from Task where Id In :RelatedTaskIds]) {
    wrapperList.add(new Wrapper(a, 'Task'));
}

Thanks,
Sascha
Guillaume ZeemanGuillaume Zeeman
The list needs to be a list of ID's. What you can do is the following.
 
Map<ID, TaskRelation> relatedTasks = new Map<ID, TaskRelation>([
	SELECT taskId
	FROM TaskRelation
	WHERE relationid = :currentRecordId
]);

List<Task> = tasks [
	SELECT Id, Subject, CreatedDate, Createdby.Name, Owner.Name, RecordType.Name, Status, Who.Name, Who.Id, What.Name
	FROM Task
	WHERE Id IN :relatedTasks.keySet()
];

for( Task task : tasks ){
    wrapperList.add(new Wrapper(task, 'Task'));
}

If you create a map with a list of SObjects, Salesforce will automatically add the ID as the key for each record. Then use the keys of the map to filter your second query. 
Sascha DeinertSascha Deinert
Hello Guillaume,

I use your code, but I get no results.
I run the code with the contact ID 0031x00001AnxgwAAB this is the content from the map relatedtasks1
09:36:49:197 VARIABLE_ASSIGNMENT [23]|relatedTasks1|{"0RT1x0000094HFHGA2":{"TaskId":"00T1x00000CSmgFEAT","Id":"0RT1x0000094HFHGA2"},"0RT1x0000094HEeGAM":{"TaskId":"00T1x00000CSmdSEAT","Id":"0RT1x0000094HEeGAM"},"0RT1x0000094HF8GAM":{"TaskId":"00T1x00000CSmgAEAT","Id":"0RT1x0000094HF8GAM"}}|0x3964be86


But the Listtasks query doesn't work
09:36:49:199 SOQL_EXECUTE_BEGIN [24]|Aggregations:0|SELECT Id, Subject, CreatedDate, Createdby.Name, Owner.Name, RecordType.Name, Status, Who.Name, Who.Id, What.Name FROM Task WHERE Id = :tmpVar1
09:36:49:206 SOQL_EXECUTE_EXPLAIN [24]|Index on Task : [Id], cardinality: 3, sobjectCardinality: 29, relativeCost 0.333
 
Map<Id, TaskRelation> relatedTasks1 = new Map<Id, TaskRelation>([select taskId from TaskRelation where relationId = :currentRecordId]); 
                system.debug('size relatedtasks1 ' +relatedtasks1.size()); // 3 items
                List<Task> listtasks = [select Id, Subject, CreatedDate, Createdby.Name, Owner.Name, RecordType.Name, Status, Who.Name, Who.Id, What.Name from Task where Id IN :relatedTasks1.keySet()];
                system.debug('size listtasks ' +listtasks.size()); // 0 items
                for(Task a : listtasks) { 
                    wrapperList.add(new Wrapper(a, 'Task'));
                }
Any ideas?

Thanks,
Sascha