IN clause in SOQL not working as expected

1.4k views Asked by At

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?

1

There are 1 answers

0
Andrés Andrade On

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.