0

I am getting the error:

DEBUG|Query : Select Id from TestObj__c where CreatedDate <= 2016-07-29 00:00:00 |EXCEPTION_THROWN|[33]|System.QueryException: unexpected token: < |FATAL_ERROR|System.QueryException: unexpected token: <

Decimal noOfDays;
    CS_Custom_Settings__c cs = CS_Custom_Settings__c.getOrgDefaults();
    if(cs != null && cs.Number_of_days_Purge_Batch_Job__c != null){
        noOfDays = cs.Number_of_days_Purge_Batch_Job__c;
    }
    Date reqDate = Date.today() - Integer.valueOf(noOfDays);
    System.debug('*reqDate*'+reqDate);
    for(Purge_Batch_Job__c batchJob : Purge_Batch_Job__c.getAll().values()){
        objAPINames.add(batchJob.Object_API_Name__c);   
    }
    for(String objName : objAPINames){
        //System.debug('Query : ' + 'Select Id from ' + objName + ' where CreatedDate <= ' + reqDate);
        System.debug('Query : ' + 'Select Id from ' + objName + ' where CreatedDate <=: reqDate');
        for(sObject s : Database.Query('Select Id from ' + objName + ' where CreatedDate <=: reqDate')){ 
            recordIds.add((Id)s.Id);

Please help me out with this

1
  • 1
    DO NOT do soql queries inside a loop
    – Akram G
    Commented Jul 29, 2016 at 7:52

1 Answer 1

1

Your query in for loop is correct

Use soql same in system debug as well

System.debug('Query : ' + 'Select Id from ' + objName + ' where CreatedDate <=: reqDate');

Here I can see you are using soql inside for loop. I guess the above list objAPINames . it will contain only few sobject so it should not hit the soql governer limits

Updates

Another issue in for loop soql, before where you need to give space

for(sObject s : Database.Query('Select Id from ' + objName + ' where CreatedDate <=: reqDate'))
5
  • Still the same error DEBUG|Query : Select Id from TestObj__cwhere CreatedDate <=: reqDate EXCEPTION_THROWN|[33]|System.QueryException: unexpected token: < |FATAL_ERROR|System.QueryException: unexpected token: <
    – wizard
    Commented Jul 29, 2016 at 7:54
  • @wizard check my updates.. you need to give space before where
    – Ratan Paul
    Commented Jul 29, 2016 at 7:58
  • I have edited my code n I am getting date formatting issue. I am gettin DEBUG|*reqDate*2016-07-29 00:00:00 but i want it in 2016-07-29T08:00:04.000+0000 format. How can i do it
    – wizard
    Commented Jul 29, 2016 at 8:27
  • @wizard can you please ask a new question
    – Ratan Paul
    Commented Jul 29, 2016 at 8:54
  • asked salesforce.stackexchange.com/questions/133638/…
    – wizard
    Commented Jul 29, 2016 at 8:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .