2

I have the following scheduled Apex code, I want to try it out on the developer console, as salesforce scheduler offers to the run the code, once an hour only.

global class examExpiryAlert implements Schedulable {

    global void execute(SchedulableContext ctx) {
        List<Exam__c> objects = [
            SELECT Name, Exam_state__c, Expiration_Date_WF__c, day7Alert__c 
            FROM Exam__c
            WHERE Exam_state__c = 'Active'];

        for(Exam__c e : objects){
            if(e.Expiration_Date_WF__c >= Date.today()-7){  
                e.day7Alert__c = True;
            } 
        }
        update objects;
    }
}

how can i run this code in the developer console to execute it right away, what parts do i have to remove or add?

5

Try:

new examExpiryAlert().execute(null);

context doesn't matter.

2

You can create a cron expression to execute scheduleable class from Developer console.

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 + ' * * ?';

MyScheduledJob s = new MyScheduledJob(); 
System.schedule('Job Started At ' + String.valueOf(Datetime.now()), nextFireTime, s);

Schedule Apex

1

If you want to execute just a part of the class, you can extract the code and run that. However, Non atomic's way is best if you want all the functionality.

List<Exam__c> objects = [
SELECT Name, Exam_state__c, Expiration_Date_WF__c, day7Alert__c 
FROM Exam__c
WHERE Exam_state__c = 'Active'];

for(Exam__c e : objects){
    if(e.Expiration_Date_WF__c >= Date.today()-7){  
        e.day7Alert__c = True;
    } 
}
update objects;
  • this is also a good answer – Niveth Kumar Mar 18 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.