1

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".

Elo
  • 226
  • 5
  • 19

2 Answers2

1

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.

David Reed
  • 2,522
  • 2
  • 16
  • 16
  • Ok, thank you. With IN is without errors. But it return nothing. What shape should the variable states be in? `String states = 'California,New York'; String states = '(California,New York)'; String states = '\'California\',\'New York\''; String states = '(\'California\',\'New York\')';` Or something else? Because it return nothing. – Elo Mar 24 '21 at 17:58
  • List of String this: `List states = new List{'California','New York'};` and also this: `List states = new String[]{'California','New York'};` is with error in SELECT: **Invalid bind expression type of List for column of type String** `//-----------------------------------------------------------------------` For set `Set states = new Set{'California','New York'};` is similar error: **Invalid bind expression type of Set for column of type String** SELECT with IN nothing return. Any Idea? – Elo Mar 25 '21 at 11:21
  • This is OK, return results: `SELECT Id, Name, BillingState FROM Account WHERE BillingState IN ('California','New York') //-------------------------------------------------------------------------------------` But this return nothing: `String states = '/'California/',/'New York/''; SELECT Id, Name, BillingState FROM Account WHERE BillingState IN (:states)` Why? I don't understand. – Elo Mar 25 '21 at 12:40
1

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.

Elo
  • 226
  • 5
  • 19