Debugging Schedulable Job in apex salesforce
Asked Answered
R

3

3

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

Retard answered 17/2, 2013 at 12:47 Comment(0)
S
4

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)

Seagrave answered 17/2, 2013 at 18:21 Comment(0)
U
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.

Ulm answered 18/2, 2013 at 10:14 Comment(3)
Best (maybe I should call it "best") solution I saw was 12 jobs sheduled to run in 1 h interals, kicked of at 5, 10, 15... minutest past full hour. If you're forced to that path you should seriously consider redesigning your app... maybe what you need is a trigger and not a sched. job, maybe streaming API if it's about notifying external systems... but as it is it smells of performance killer and deeper design problems lurking.Seagrave
When I was developing application which integrates one of project management system into salesforce I needed to synchronize with this system as frequently as possible. We ended up with every 15 minute - it's 4 jobs. We decided that this will be enough for us for that time. And it was working just perfectly.Ulm
@chiz I have spent too much time trying to manage scheduled jobs from a scheduled job. It fails silently when trying to remove/abort jobs. I didnt try to add a job though.Deannadeanne
A
0

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());
Anthroposophy answered 4/10, 2013 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.