+ Start a Discussion
SainSain 

How to fetch and delete records before 30m as per shcedule time

Hi,
I have two objects doctor, appointment and there is a lookup relationship between them.  have a fields called appointment_date__c(date/time type) and doctor__c lookup field in Appointment.
Now i have to delete the Appointment records those are Schedule is 'Confirm'(Doctor object field) and current time is 30m before from appointment_date__c field time.

for suppose 5 records appointment time is 6:30PM and current time is 6:00PM, this 5 records have to fetch and delete.

My Batch Class:

Global class AppointmentDelete implements database.Batchable<sObject>{
    global database.QueryLocator start(database.BatchableContext BC){
    string c='Confirm';
    String query='Select id,Name, doctor__r.name from Appointment__C where doctor__r.Schedule__c =\'' + c + '\'';
    system.debug('>>>>>>>query: '+query);
    return database.getQueryLocator(query);
    }
    
    global Void execute(database.BatchableContext BC, List<Appointment__C> scope ){
            
        delete scope;
       
    }
    
    global void finish(database.BatchableContext BC){
        
    }
}

 
Best Answer chosen by Sain
Madhanprabhu Thangadurai 1Madhanprabhu Thangadurai 1
Hi Pankaj,

I think in the filter criteria, it should be appointment_date__c >= d and appointment_date__c <= dnow.
 
Global class AppointmentDelete implements database.Batchable<sObject>{
    global database.QueryLocator start(database.BatchableContext BC){
    string c='Confirm';
    Datetime d = datetime.now().addMinutes(-30);
    Datetime dnow = datetime.now();
    String query='Select id,Name, doctor__r.name from Appointment__C where doctor__r.Schedule__c =\'' + c + '\' AND appointment_date__c <= dnow AND appointment_date__c >= d';
    system.debug('>>>>>>>query: '+query);
    return database.getQueryLocator(query);
    }
    
    global Void execute(database.BatchableContext BC, List<Appointment__C> scope ){
            
        delete scope;
       
    }
    
    global void finish(database.BatchableContext BC){
        
    }
}

 

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,

Datetime d = datetime.now().addMinutes(-30);
Datetime dnow = datetime.now();
String query='Select id,Name, doctor__r.name from Appointment__C where doctor__r.Schedule__c =\'' + c + '\' AND appointment_date__c <= d AND appointment_date__c > dnow';

Can you please try to use above soql?
Madhanprabhu Thangadurai 1Madhanprabhu Thangadurai 1
Hi Pankaj,

I think in the filter criteria, it should be appointment_date__c >= d and appointment_date__c <= dnow.
 
Global class AppointmentDelete implements database.Batchable<sObject>{
    global database.QueryLocator start(database.BatchableContext BC){
    string c='Confirm';
    Datetime d = datetime.now().addMinutes(-30);
    Datetime dnow = datetime.now();
    String query='Select id,Name, doctor__r.name from Appointment__C where doctor__r.Schedule__c =\'' + c + '\' AND appointment_date__c <= dnow AND appointment_date__c >= d';
    system.debug('>>>>>>>query: '+query);
    return database.getQueryLocator(query);
    }
    
    global Void execute(database.BatchableContext BC, List<Appointment__C> scope ){
            
        delete scope;
       
    }
    
    global void finish(database.BatchableContext BC){
        
    }
}

 
This was selected as the best answer
Pankaj_GanwaniPankaj_Ganwani
Hi Madhan,

Thanks for pointing this out. This was the minor mistake from my end. I appreciate your heads up here.

Thanks,
Pankaj
SainSain
Thanks for your support Pankaj and Madhan, sorry for late reply.