0

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);
}
2

There's special magic in SOQL, and it goes like this:

Code:

select id, whatid from task where what.type = 'Case'

Hope this helps...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.