You need to sign in to do that
Don't have an account?
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); }
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
@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
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
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