1
votes

What is wrong at this code in Apex?

String states = 'California,New York';
List<Account> lstACC = [SELECT Id, Name, BillingState FROM Account WHERE BillingState INCLUDES (:states) LIMIT 10];

In Developer Console is an Error: "BillingState FROM Account WHERE BillingState INCLUDES (:states)^ERROR at Row:1:Column:50 includes or excludes operator only valid on multipicklist field".

2

2 Answers

1
votes

The text of the error shows the problem:

includes or excludes operator only valid on multipicklist field

BillingState is not a multi-select picklist. Use IN rather than INCLUDES to match against the collection.

Note additionally that a comma-separated string is not a valid collection to match against. Create a List<String> or Set<String> to use in your matching condition.

1
votes

The right solution:

Set<String> setStates = new Set<String>();
setStates.add('California');
setStates.add('New York');
List<Account> lstACC = [SELECT Id, Name, BillingState 
                        FROM Account 
                        WHERE BillingState IN :setStates 
                        LIMIT 10];

Wrong:

setStates: {'California','New York'}

Right:

setStates: {California,New York}

Apostrophes are in addition. OR

String states = 'California,New York';
List<String> listStates = states.split(',');
List<Account> lstACC = [SELECT Id, Name, BillingState 
                        FROM Account 
                        WHERE BillingState IN :listStates 
                        LIMIT 10];

Wrong:

String states = '\'California\',\'New York\'';

Right:

String states = 'California,New York';

SOQL injection is in addition.