I have a SOQL query that is returning records that should not be returned.
As a little backstory - To solve this, I added on a IN
clause to the end of my query but I am getting "Entity 'Task' is not supported for semi join inner selects". I found this post and attempted to loop through a list outside of the query and then use a binded variable but am getting the same results.
The main issue - I found this is because the WhatId on Task is returning the same value as the AccountId on Task. I need the WhatId to be a lookup to Case.
orderedTaskList1 is the ideal query but because WhatId is returning the AccountId AND I can't do semi join inner selects, I am not getting back the correct results.
I have to do a nested query because I need the CaseNumber field along with all the Task fields.
Is there a way to go from Task -> Case?
orderedTaskList1 = [SELECT (SELECT CreatedDate, Comments__c, OwnerId, Subject, Owner.Name, AccountId, WhatId, CreatedById, Account.Name FROM Tasks WHERE Private__c = FALSE AND (Status = 'Completed' OR Status = 'Closed') ORDER BY CreatedDate DESC), AccountId, CaseNumber FROM Case WHERE AccountId = :strAccountId AND Id IN (SELECT WhatId FROM Task WHERE AccountId = :strAccountId)];
orderedTaskList2 = [SELECT (SELECT CreatedDate, Comments__c, OwnerId, Subject, Owner.Name, AccountId, WhatId, CreatedById, Account.Name FROM Tasks WHERE Private__c = FALSE AND (Status = 'Completed' OR Status = 'Closed') ORDER BY CreatedDate DESC), AccountId, CaseNumber FROM Case WHERE AccountId = :strAccountId AND Id IN :accountIds];
for(Task task : [SELECT WhatId, AccountId FROM Task WHERE AccountId = :strAccountId]) {
accountIds.add(task.WhatId);
}