function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hermann OuréHermann Ouré 

Reference Set<Id> in Dynamic SOQL in Batch class

Hello,
I wrote a batch and I am trying to reference a Set<Id> in the WHERE clause but I have an error:
System.QueryException: expecting a colon, found '{kA09E0000005mCySAI}'

How can I fix the error and reference my Set kesIds in the Dynamic SOQL?
Thanks

global class BatchArticleEmail implements Schedulable, Database.Batchable<SObject> {
    
    global string query;
    
    // To schedule batch 
    global void execute(SchedulableContext sc) {
		Id batchProcessId = Database.executeBatch(this);
	}

    // Constructor to query articles
    global BatchArticleEmail() {
        
        Set<Id> kesIds = new Set<Id>();
       
        List<Known_Error_Subscription__c> kesList = [SELECT KnowledgeKA__c FROM Known_Error_Subscription__c];
        for(Known_Error_Subscription__c kes : kesList) {
            kesIds.add(kes.KnowledgeKA__c);
        }
        
        query = 'Select Id, KnowledgeArticleId, Known_Error_Status__c FROM Knowledge__kav WHERE PublishStatus=\'Online\'' + 
                ' AND KnowledgeArticleId IN \''+kesIds+'\'';     
        System.debug('query BatchArticleEmail ' +query);
    }
Vishwajeet kumarVishwajeet kumar
Hello,
You should be able to use variable in query by itself.
Try below : 
 query = 'Select Id, KnowledgeArticleId, Known_Error_Status__c FROM Knowledge__kav WHERE PublishStatus=\'Online\'' + ' AND KnowledgeArticleId IN :kesIds';

Thanks
Hermann OuréHermann Ouré
Hello,
@Vishwajeet
Thanks for your reply.
That does not work either
What you posted was the first thing I tried but when using :kesIds

query = 'Select Id, KnowledgeArticleId, Known_Error_Status__c FROM Knowledge__kav WHERE PublishStatus=\'Online\'' + ' AND KnowledgeArticleId IN :kesIds';

I have another error:
Variable kesIds does not exist


 
Vishwajeet kumarVishwajeet kumar
Ok some times variable do not get recognized in dynamic query (have seen this error before).Try to put kesIds in bracket seperated by comma - ('Id1','id2'..) and use.You can get rid of Set variable and build a where clause string for it if set is not going to be used in other places.

Somthing like this: 

String v_KesIdWhereClause = '(';
for(Known_Error_Subscription__c kes : kesList) 
  v_KesIdWhereClause += '\'' + kes.KnowledgeKA__c + '\',';        //format - 'Id'

v_KesIdWhereClause = v_KesIdWhereClause.substring(0,v_KesIdWhereClause.length() - 1);          //remove last ','
v_KesIdWhereClause += ')';


query = 'Select Id, KnowledgeArticleId, Known_Error_Status__c FROM Knowledge__kav WHERE PublishStatus=\'Online\'' + ' AND KnowledgeArticleId IN ' + v_KesIdWhereClause ;


Thanks
 
Omar Rajab 94Omar Rajab 94
Hi Hermann, 
Please try the following: 
- Define the "kesIds" as private variable.
private  Set<Id> kesIds = new Set<Id>();
- write the query string like below:
 query = 'Select Id, KnowledgeArticleId, Known_Error_Status__c FROM Knowledge__kav WHERE PublishStatus=\'Online\'' + ' AND KnowledgeArticleId IN :this.kesIds';

Thanks