1

I tried to get all related tasks from the selected contact, but salesforce don't like my code. If I try to save I get the error: Invalid bind expression type of Task for column of type Id

What I'm doing wrong?

          List<Taskrelation> RelatedTasks = [select taskId from taskrelation where relationid =: currentRecordId];
            for (task rt : RelatedTasks) {
                for(Task a: [select Id, Subject, CreatedDate, Createdby.Name, Owner.Name, RecordType.Name, Status, Who.Name, Who.Id, What.Name from Task where Id =:rt]) { 
                    wrapperList.add(new Wrapper(a, 'task'));
                } 
            }
1
  • TaskRelation is a different object than Task -- you can't compare their Ids. Also you have a SELECT statement inside the outer for loop so you are going to have problems with governor limits. Commented Sep 14, 2021 at 23:50

1 Answer 1

1

First coming to the issue

[select Id, Subject, CreatedDate, Createdby.Name, Owner.Name, RecordType.Name, Status, Who.Name, Who.Id, What.Name from Task where Id =:rt]

Here rt is sObject , whereas it should be Id

[select Id, Subject, CreatedDate, Createdby.Name, Owner.Name, RecordType.Name, Status, Who.Name, Who.Id, What.Name from Task where Id =:rt.Id]

Secondly, you should always avoid SOQL in for loop. You can move this query outside loop and pass relatedTaskIds. And insert data to relatedTaskIds after looping through RelatedTasks for eg:

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'));
}
2
  • Just want to add to this that you can use Id =: rt where rt is sObject or list of sObjects, in this case it will use the Ids of those sObjects. That is the reason why it is throwing error on Invalid bind. On left side you have Task Id and ont the right side you have RelatedTask Id.
    – Damecek
    Commented Sep 14, 2021 at 8:19
  • @Nagendra, I tried to use your second way, but I get also an error: Invalid bind expression type of TaskRelation for Id field of SObject Task ||| 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')); }
    – peXeq
    Commented Sep 14, 2021 at 9:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .