Salesforce、Apex、SOQL、SELECT 包含 INCLUDES

Salesforce, Apex, SOQL, SELECT with INCLUDES

Apex 中的这段代码有什么问题?

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

在开发人员控制台中出现错误:“BillingState FROM Account WHERE BillingState INCLUDES (:states)^ERROR at Row:1:Column:50 includes or excludes operator only valid on multipicklist field”.

错误文本显示问题所在:

includes or excludes operator only valid on multipicklist field

BillingState 不是多 select 选择列表。使用 IN 而不是 INCLUDES 来匹配集合。

另外请注意,逗号分隔的字符串不是要匹配的有效集合。创建一个 List<String>Set<String> 以在您的匹配条件中使用。

正确的解决方案:

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];

错误:

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

右:

setStates: {California,New York}

还有撇号。 或者

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];

错误:

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

右:

String states = 'California,New York';

另外还有SOQL注入