1
global Database.QueryLocator start(Database.BatchableContext BC)
{
    String query = 'select id,ParticipantName__c,EpisodePoPEndDate__c from BPCIAe__c where id in :CHildParentMap.keyset() OR id in :ParentWithdrawaldateMap.keyset()  and (EpisodePoPEndDate__c=null OR PoPEndDate__c > TODAY)';
    return Database.getQueryLocator(query);
}

Unexpected token ( is thrown at the Database.Querylocator.

2

You can't use any sort of dot reference in a dynamic bind variable. So anywhere you are calling .keySet(), you would need to cache that Set to be referenced directly.

Invalid

String query = '...WHERE Id IN CHildParentMap.keyset() OR Id IN :ParentWithdrawaldateMap.keyset()'

Valid

Set<Id> ids = new Set<Id>();
ids.addAll(childParentMap.keySet());
ids.addAll(parentWithdrawalDateMap.keySet());
String query = '...WHERE Id IN :ids'

A side benefit in this case is it allows you to eliminate an OR clause, which is always a win.


For your reference:

Dynamic SOQL Considerations

You can use simple bind variables in dynamic SOQL query strings. The following is allowed:

String myTestString = 'TestName';
List<sObject> sobjList = Database.query('SELECT Id FROM MyCustomObject__c WHERE Name = :myTestString');

However, unlike inline SOQL, dynamic SOQL can’t use bind variable fields in the query string. The following example isn’t supported and results in a Variable does not exist error:

MyCustomObject__c myVariable = new MyCustomObject__c(field1__c ='TestField');
List<sObject> sobjList = Database.query('SELECT Id FROM MyCustomObject__c WHERE field1__c = :myVariable.field1__c');

You can instead resolve the variable field into a string and use the string in your dynamic SOQL query:

String resolvedField1 = myVariable.field1__c;
List<sObject> sobjList = Database.query('SELECT Id FROM MyCustomObject__c WHERE field1__c = ' + resolvedField1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.