0

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<Sobject>{

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<monitorlog__c> scope){
    List<monitorlog__c> monitorObjList = new List<monitorlog__c>();
    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

2

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

  • And, the variable must be in scope-- it should be set within the batch class so the query can access it. – sfdcfox Jun 24 '15 at 14:58

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.