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:
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);