I was trying to build a query to pass it in the QueryLocator. The following is the code snippet I tried to generate the dynamic query,
public String buildQueryString(Object_Rule__mdt ObjectRule, Datetime createdDatetime) {
List<String> fields = new List<String>();
Set<String> statusSetStrings = new Set<String>{'Pending', 'Processing'};
this.query = 'SELECT ';
for (Field_Rule__mdt fieldRules : ObjectRule.Field_Rules__r) {
fields.add(fieldRules.Field_Name__c);
}
this.query += String.join(fields, ', ') + ', ' + ContactIdField__c + ' FROM ' + ObjectRule.SObject__c + ' WHERE ' + ObjectRule.ContactIdField__c
+ ' IN (SELECT Contact__c FROM Request__c WHERE Status__c IN :statusSetStrings AND CreatedDate < :createdDatetime)';
return this.query;
}
When I tried the above query in a Batch then the batch is returned the error as 'Variable does not exist :statusSetStrings' I was trying to change the query as without the ':' as well before statusSetStrings.
What did I missed?