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
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){
        
    }
}

 
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 <= dnow AND appointment_date__c>=d';

Can you please try to use above soql?