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
marys pindamarys pinda 

Compare system hour with time Job Schedulable

Is it possible to compare the system time with the time for a JOB?
Something like:

global class AAAExporterCSV implements System.Schedulable {
global void execute(SchedulableContext sc) {
List<case> caseFermeList = [Select casenumber,subject,Contact.Name,status from case where closeddate=TODAY];
string titre= 'Id, Subject,Demandeur,Status'+'\n';
string contenuCSV = titre;
for(Case a: caseFermeList )
if(DateTime.Now() == SCHEDULABLEJOB.Time){
if(a.closeddate == system.Today()){
{
   string contenu = a.casenumber + ',' + a.subject+ ',' +a.Contact.Name+ ',' + a.status +'\n';
   contenuCSV = contenuCSV + contenu ;
}
Messaging.EmailFileAttachment csvPJ = new Messaging.EmailFileAttachment();
blob csvBlob = Blob.valueOf(contenuCSV);
string csvNom = 'cases_fermes.csv';
csvPJ.setFileName(csvNom);
csvPJ.setBody(csvBlob);
Messaging.SingleEmailMessage email =new Messaging.SingleEmailMessage();
String[] adressMail = new list<string> {'v.u@uoly.com'};
String subject = 'report';
email.setSubject(subject);
email.setToAddresses( adressMail );
email.setPlainTextBody('Hi ....');
email.setFileAttachments(new Messaging.EmailFileAttachment[]{csvPJ});
Messaging.SendEmailResult [] envoyer = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
  }
  }
  }
}



Thank you!
sravan velamasravan velama
Hi 

I think you can do it by querying For Example : CronTrigger ct = [SELECT TimesTriggered, EndTime, NextFireTime FROM CronTrigger WHERE Id = :sc.getTriggerId()];

Here we can get Job id from sc.getTriggerId(), if we write this query inside exceute method.

The EndTime tells at which time the job got finished or will finish.
The NextFireTime  tells the  next date and time the job is scheduled to run. Null, if the job is not scheduled to run again.
Please follow the Link: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_crontrigger.htm