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
Arjun y 7Arjun y 7 

Unable to query the records using dynamic query

Hi All,

Unable to query the records using dynamic query. Can any one help me to solve the below query..
Here selectionApiname and SelectionFieldId's are dynamic variables. 

if(id!=null){
selectionApiname = 'Id';
SelectionFieldId = ''\''001..'\'''
}
else{
selectionApiname = 'Name';
SelectionFieldId = ''\''TestName'\''';
}
Please find the below code

Datetime createdstartdate =2015-01-10T12:00:00Z ;
DateTime createdenddate =2015-01-30T12:00:00Z ;
String selectionApiname = 'Id';
String SelectionFieldId = '001.....'

String accountRecords = 'select Id From account'
                              + 'where (createdDate >= '
                              +createdstartdate.format('yyyy-MM-dd') + 'T' +     createdstartdate.format('hh:mm:ss') +   'Z'
                              + ' and createdDate <='
                              +createdenddate.format('yyyy-MM-dd') + 'T' + createdenddate.format('hh:mm:ss') + 'Z'+')'
                              + ' or '+'('+selectionAPIName+ ' In '+selectionFieldid+')'
                              +'limit 10';
List<Account> acc = Database.query(accountRecords);
doravmondoravmon
what's that T/Z ? You might missed some branket there.