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
maheswar reddy 36maheswar reddy 36 

write a batch apex to delete accounts where there are no bookings in last two years?Relationship : Account-->passenger-->Booking

Raj VakatiRaj Vakati
Change the API names in the below SOQL 
 
global class BatchDelete implements Database.Batchable<sObject> {
   public String query;

   global Database.QueryLocator start(Database.BatchableContext BC){
	   String query ='Select Id from Account where passenger__r.Booking__r.Date__c < LAST_N_YEARS:2'
	   
      return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope){
      delete scope;
      DataBase.emptyRecycleBin(scope);
   }

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