Debugging Schedulable Job in apex salesforce

2019-05-26 09:34发布

问题:

i am trying to run a schedulable job i never used schedulable jobs in salesforce

here is my code

global class scheduledMerge implements Schedulable{
   global void execute(SchedulableContext SC) {
      System.debug('Hello World'); 
   }
}

          created a cusom controller
public class schedulableMerge{

public schedulableMerge(){

}
public PageReference Hello(){
scheduledMerge m = new scheduledMerge();
        String sch = '0 10 * * 1-12 ? *';
        system.schedule('Merge Job', sch, m);
        return null;
}

}

and visualforce page is

<apex:page controller="schedulableMerge">
  <!-- Begin Default Content REMOVE THIS -->
    <h1>Congratulations</h1>
  This is your new Page
  <!-- End Default Content REMOVE THIS -->
  <apex:form >
  <apex:commandButton value="Press it" action="{! Hello}" />
</apex:form>

</apex:page>

when i press button press it there is a job in All schedulable job but there is only one option delete with it.No option of manage. i think this job should run in every 10 minutes .i saw debug logs from Monitoring>Debug Log it is showing no log. can you please tell what is the expression for running job in every one minute and where to see debug logs?? my aim is to see working of schedulable job

回答1:

Good news - you don't need a visualforce page and controller. Bad news - you can't schedule jobs with 1 minute intervals. I think 5 mins is the minimum (but I'm not sure, you'd have to experiment with it).

How to run it (once) on demand? From Developer Console or Eclipse's "Execute Anonymous" block.

Make sure your user is added to debug logs and simply force running of the execute method you had to implement as part of the interface:

scheduledMerge sm = new scheduledMerge();
sm.execute(null);

Experiment with the cron expressions after you're satisfied that one run completes succesfully. If you're fine with frequency once a day - you don't need these expressions at all, go to Setup -> Develop -> Classes and click [Schedule Apex]. Only if you need multiple runs a day use code to schedule the class.

Last but not least - go to setup and type "Apex jobs" in the search. You should see info of all asynchronous tasks performed recently (scheduled jobs, batches, @future methods etc)



回答2:

can you please tell what is the expression for running job in every one minute and where to see debug logs??

You can't run it every minute. Minimum is ONE HOUR - Apex Scheduler. There is one huck - to run job as frequently as it possible to start a lot of jobs.

You can try to schedule new job in currently running job (if it's possible) to run every Datetime.now().minute() + 1. This idea just poped up in my mind. I didn't try it. So the same class will start in one minute. But don't forget to kill current job in this class so you don't create huge number of scheduled jobs and run out of governor limits.



回答3:

I have done something like this for Scheduled Apex to run for 15,30,45,60 min (ie, every 15 mins)

The downside is, we need to create 4 different Scheduled Apex Jobs for this to execute.

System.schedule('Every 0th min', '0 0 * * * ?', new scheduledMerge());
System.schedule('Every 15th min', '0 15 * * * ?', new scheduledMerge());
System.schedule('Every 30th min', '0 30 * * * ?', new scheduledMerge());
System.schedule('Every 45th min', '0 45 * * * ?', new scheduledMerge());