• Mohd Rizvan
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
I'm trying to construct a query for use in a trigger that uses a multi-select picklist field in the WHERE clause.  I started by hardcoding the choices in the where clause and that worked fine.  I then tried to dynamically get the choices and put them in a string called DeleteDepartmentsString.  The 2 strings DynamicQuery and HardCodedQuery (see below), appear to be identical according to the debug logs.  However, when I change Database.query(HardCodedQuery) to Database.query(DynamicQuery), I get the error System.QueryException: unexpected token: ')'

Can't figure it out what's different about the query strings.  What is the proper way to choose multiple selections from a multi-select picklist in the WHERE clause?  Thanks in advance.

Relevent code is below(debug log lines in bold).

System.debug(DeleteDepartmentsString);
15:14:20.276 (276302000)|USER_DEBUG|[101]|DEBUG|'Intellectual Property', 'Information Technology'

  String DynamicQuery = 'SELECT Id FROM Partner_Course__c WHERE Controlled_Document__c = \'a5fA00000004EJ2IAM\' AND Name__r.Department_MultiSelect__c INCLUDES ('+  DeleteDepartmentsString + ') AND Name__r.Department_MultiSelect__c EXCLUDES (\'Executive\') AND Course_Status__c = \'Enrolled\'';

  String HardCodedQuery = 'SELECT Id FROM Partner_Course__c WHERE Controlled_Document__c = \'a5fA00000004EJ2IAM\' AND Name__r.Department_MultiSelect__c INCLUDES (\'Intellectual Property\', \'Information Technology\')  AND Name__r.Department_MultiSelect__c EXCLUDES (\'Executive\') AND Course_Status__c = \'Enrolled\'';
 
  List<Partner_Course__c> CD = Database.query(HardCodedQuery);
15:14:20.276 (276700000)|SOQL_EXECUTE_BEGIN|[111]|Aggregations:0|SELECT Id FROM Partner_Course__c WHERE Controlled_Document__c = 'a5fA00000004EJ2IAM' AND Name__r.Department_MultiSelect__c INCLUDES ('Intellectual Property', 'Information Technology')  AND Name__r.Department_MultiSelect__c EXCLUDES ('Executive') AND Course_Status__c = 'Enrolled'
15:14:20.308 (308539000)|SOQL_EXECUTE_END|[111]|Rows:6

 
System.debug(DynamicQuery);
15:14:20.308 (308676000)|USER_DEBUG|[113]|DEBUG|SELECT Id FROM Partner_Course__c WHERE Controlled_Document__c = 'a5fA00000004EJ2IAM' AND Name__r.Department_MultiSelect__c INCLUDES ('Intellectual Property', 'Information Technology') AND Name__r.Department_MultiSelect__c EXCLUDES ('Executive') AND Course_Status__c = 'Enrolled'
  System.debug(HardCodedQuery);
15:14:20.308 (308694000)|USER_DEBUG|[114]|DEBUG|SELECT Id FROM Partner_Course__c WHERE Controlled_Document__c = 'a5fA00000004EJ2IAM' AND Name__r.Department_MultiSelect__c INCLUDES ('Intellectual Property', 'Information Technology')  AND Name__r.Department_MultiSelect__c EXCLUDES ('Executive') AND Course_Status__c = 'Enrolled'