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?

up vote 4 down vote accepted

Try:

new examExpiryAlert().execute(null);

context doesn't matter.

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

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;

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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