Hello friends, In this post, I will let you know, how to scheduled an apex class using value of datetime field of sObject. In this section i am building a CRON expression using datetime field value that used to scheduled an apex class.
Scheduled a class as per date/time field value
For this implementation, I have created a datetime field on Account object named "Scheduled Date/Time". So the sample code is:
Account acc=[SELECT Id,ScheduledDateTime__c FROM Account WHERE ScheduledDateTime__c!=null limit 1];
String sDay=String.valueof(acc.ScheduledDateTime__c.day());
String sHour=String.valueof(acc.ScheduledDateTime__c.hour());
String sMinute=String.valueof(acc.ScheduledDateTime__c.minute());
String sMonth=String.valueof(acc.ScheduledDateTime__c.month());
String sYear=String.valueof(acc.ScheduledDateTime__c.year());
String CRON_EXP = '0 '+sMinute+' '+sHour+' '+sDay+' '+sMonth+' ? '+sYear;
String jobID = System.schedule('ScheduledWithDateTime', CRON_EXP, new SampleScheduler());
System.debug('jobID^^'+jobID);
To execute the above code please navigate to Developer Console | Debug | Open Execute Anonymous Window, paste the code there and click on "Execute" button.
Example:
Output:
You can see the class has been scheduled on selected date/time value of the field.Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.
0 Comments
Post a Comment