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
David SilvaDavid Silva 

How to pass an sObject to batch apex

global class DeleteJob implements Database.Batchable<sObject>{

   global final Integer noOfYears;
    sObject recordDeletionObject;

   global DeleteJob(sObject newobject,Integer noOfYears)
   {
      recordDeletionObject= newObject;
       this.noOfYears=noOfYears;
   }

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

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

   global void finish(Database.BatchableContext BC){
   }
}
CloudGeekCloudGeek
Hello Daid,

Looks like you are trying to implement similar to this :
https://developer.salesforce.com/forums/?id=906F0000000AtLQIA0

You can try something like :
//Suppose your sObject is Account
Account acc = new Account(Name = 'Test Account');
​insert acc;
​
DeleteJob dj = new DeleteJob(acc,2);  //object and years passed to your constructor
dj.executeBatch(dj);