I have a method where I need to schedule an apex class that will close several Cases in 5 days but I need to somehow pass Case Id so the scheduled class knows which cases to remove after 5 days. How to do it?
Now schedule method looks like this but CloseInactiveCaseSchedulable is empty
public static void scheduleCloseCaseJob(List<Case> cases, Map<Id, Case> oldCases) {
for (Case c : cases) {
if (c.Status == 'Waiting for customer reply' || c.Status == 'Waiting for customer approval') {
String day = String.valueOf(Datetime.now().addDays(20).day());
String hour = String.valueOf(Datetime.now().hour());
String min = String.valueOf(Datetime.now().minute());
String ss = String.valueOf(Datetime.now().second());
//parse to cron expression
String nextFireTime = ss + ' ' + min + ' ' + hour + ' ' + day +' * ?';
System.schedule('Close Inactive Case in 20 days with Id ' + c.Id, nextFireTime, new CloseInactiveCaseSchedulable());
}
}
}
It doesn't have to be like that:
System.schedule('Close Inactive Case in 20 days with Id ' + c.Id, nextFireTime, new CloseInactiveCaseSchedulable());
You can pass any parameters you want, to constructor
Set<Id> ids = new Set<Id>{'005....', '005...'};
System.schedule('Close Inactive Case in 20 days with Id ' + c.Id,
nextFireTime,
new CloseInactiveCaseSchedulable(ids)
);
Or have a parameterless constructor and pass it after
CloseInactiveCaseSchedulable job = new CloseInactiveCaseSchedulable();
job.ids = new Set<Id>{'005....', '005...'};
System.schedule('Close Inactive Case in 20 days with Id ' + c.Id,
nextFireTime,
job
);
The Schedulable interface enforces on you only the existence of the execute
method. Your class can have no constructor, constructor without params or with params, whatever you need.
(Don't schedule them in a loop though. Collect the IDs in the loop and then have 1 System.schedule()
call outside of it).