asitm9
asitm9

Reputation: 953

IN clause in SOQL not working as expected

Code snippet1:

list<string> abc = new list<string>{'a1V44000008TyEEEA0'};  
list<task> t = [ SELECT Id FROM Task WHERE (whatId in :abc or Whoid in :abc)]
system.debug('-->'+t.size());

Above code segment is working as expected.

Code snippet2:

list<string> abc = new list<string>{null, 'a1V44000008TyEEEA0'};  
list<task> t = [ SELECT Id FROM Task WHERE (whatId in :abc or Whoid in :abc)]
system.debug('-->'+t.size());

Snippet2 is returning a result as if there is no WHERE clause. What is the logic behind this or is it any bug?

Upvotes: 0

Views: 1543

Answers (1)

Andr&#233;s Andrade
Andr&#233;s Andrade

Reputation: 2223

The query in your second code snippet will return all task records where whatId or WhoId equals null or a1V44000008TyEEEA0. Probably in most of your task records whatId or WhoId is NULL or a1V44000008TyEEEA0.

You can execute:

list<task> t = [ SELECT Id FROM Task WHERE (whatId != NULL or Whoid != NULL)]

to check if there is any task where both whatId and Whoid is not null.

Upvotes: 2

Related Questions