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
App DevelopmentApp Development 

Gettting the issue while running batch apex from apex class

I have developed one class which runs on REST webservice and called batch apex for more than 10000 records deletions for the same.
Right now its showing error while fetching query by parameter.
Could you please help on this?  any suggestions?

Apex class:

@RestResource(urlMapping='/PurgeMonitorLog/*')
global with sharing class PurgeMonitorlogRestService {
    @HttpPost
    global static Boolean deleteMonitorLogEntries(String lastExtractedTimeUTC) {
        Boolean isDeleted=false;
        try{
            
            if(lastExtractedTimeUTC == NULL || lastExtractedTimeUTC == ''){
                System.debug('Passed time is Null or empty, Purge process cant proceed: '+lastExtractedTimeUTC);
                return isDeleted;
            }
            
            String[] timeArray=lastExtractedTimeUTC.split('T');
            String transformedTime=timeArray[0]+' '+timeArray[1];
          
            DateTime dtLET_UTC=DateTime.valueOfGmt(transformedTime);
            System.debug('Get all records before time (in UTC):'+dtLET_UTC);
            
            
            String Query;
            Query = 'select id from monitorlog__c where lastmodifieddate < :'+dtLET_UTC;
            
            database.executebatch(new  DeleteMonitorlogBatchClass(Query));
    
   
        }catch (Exception ex){
            System.debug(Logginglevel.ERROR, 'deleteMonitorLogEntries: Exception while deleting MonitorLog Records due to: ' + ex.getMessage());
            MonitorUtility.insertHandledExceptions(ex, null, 'MonitorLog', 'PurgeMonitorlogRestService','PurgeMonitorLog','GSD','Monitoring',System.now());
        }  
        System.debug('returned val:'+isDeleted);
        return isDeleted;
    }
    
}

Batch Apex:

global class DeleteMonitorlogBatchClass implements Database.Batchable{

    global final string Query;
    
    global DeleteMonitorlogBatchClass(String q){
        Query = q;
    }

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(Query);
    }

    global void execute(Database.BatchableContext BC, List scope){
        List monitorObjList = new List();
        for(monitorlog__c Mon : scope){
            monitorlog__c MonInst = (monitorlog__c)Mon;
            monitorObjList.add(MonInst);
        }
        
        if(!monitorObjList.isEmpty()){
            Database.delete(monitorObjList, false);
            Database.emptyRecycleBin(monitorObjList); 
        }
    }

    global void finish(Database.BatchableContext BC){
                    
    }
}


ERROR :

34.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
01:20:01.027 (27069598)|EXECUTION_STARTED
01:20:01.027 (27099942)|CODE_UNIT_STARTED|[EXTERNAL]|01pq0000000Eg40|DeleteMonitorlogBatchClass
01:20:01.050 (50255196)|METHOD_ENTRY|[1]|01pq0000000Eg40|DeleteMonitorlogBatchClass.DeleteMonitorlogBatchClass()
01:20:01.050 (50270556)|METHOD_EXIT|[1]|DeleteMonitorlogBatchClass
01:20:01.050 (50398704)|SYSTEM_METHOD_ENTRY|[10]|Database.getQueryLocator(String)
01:20:01.050 (50635038)|EXCEPTION_THROWN|[10]|System.QueryException: unexpected token: '2015-06-23'
01:20:01.050 (50709933)|SYSTEM_METHOD_EXIT|[10]|Database.getQueryLocator(String)
01:20:01.050 (50762663)|FATAL_ERROR|System.QueryException: unexpected token: '2015-06-23'

Class.DeleteMonitorlogBatchClass.start: line 10, column 1
Prashant WayalPrashant Wayal
Hi,
Can you try below query string. Hope it helps you.

String Query;
            Query = 'select id from monitorlog__c where lastmodifieddate < :dtLET_UTC';


Thanks
Tavant TechnologiesTavant Technologies
Try to modify your query as
Query = 'select id from monitorlog__c where lastmodifieddate < :dtLET_UTC';

If you append dtLET_UTC separatly the value will be concatenated to string. To treat this as a variable you need to include the variable name inside the query