4

Need to know , How to find a class is scheduled. Assuming since I do not see the system.schedulebatch in either of the classes that it is scheduled.

I was looking for the following changes:

  1. How do you find the frequency of a scheduled Apex Class.
  2. How would I change this code to delete once every 2 weeks instead of nightly.

Is it possible ? Please assist.

try {
    Gp junkQueue =  [select Id from Gp where DeveloperName = 'Junk' and Type = 'Queue' Limit 1];
    if(junkQueue.Id != null) {
        String query= 'Select id from Case Where OwnerId = \''+junkQueue.Id+'\'';
        Baes delCases = new Baes(query);
        Database.executebatch(delCases,100);
    }            
} catch(Exception exp) {
    System.debug('Exception :'+exp.getMessage());
}
2
  • Did you check scheduled class in setup-->Jobs--> Apex Jobs ?? @cloud – DOMINIC EDWARD Jan 6 '16 at 15:12
  • Yes I have checked and didn't find any. – cloudy-ritz Jan 6 '16 at 15:16
3

As @DOMINIC EDWARD said earlier, executed Apex jobs appear in Setup > Jobs > Apex Jobs. Also, currently scheduled Apex jobs appear in Setup > Jobs > Scheduled Jobs. If you find none, it means that there are no scheduled jobs.

Funnily enough, that UI does not allow you to create the schedules. You can schedule them with a bit of anonymous Apex, as follows:

System.schedule('Description', '0 0 22 ? * 2#1', new YourScheduledApexClass ());
System.schedule('Description', '0 0 22 ? * 2#3', new YourScheduledApexClass ());

The magic looking string encodes when to run your schedule. You can find more info here. I didn't find out how to schedule something to run twice a month, so I created 2 schedules that run on alternating forthnights.

If what you want to do is to schedule a batchable job, it's even easier:

System.scheduleBatch (new YourBatchableClass(), 'description', 5);

More info here.

If what you want to do is just run a batch ASAP, then your syntax (Database.executebatch()) seems to be OK. If you don't see any jobs that have run it's probably because you're not getting into the if clause.

Note: all this anonymous Apex should be run as the user that you want to run the scheduled jobs under.

9
  • Thanks :) I have found the apex job in scheduled jobs. How can we change the frequency and change the code to run at night. Is it needs to be done using UI again and delete the current one. – cloudy-ritz Jan 6 '16 at 16:17
  • I would deleting via the UI in Scheduled Jobs and re-create them using anonymous Apex like the one I posted above – mkorman Jan 6 '16 at 16:26
  • mkorman -- in addition to your excellent answer, you may need to do the execute anonymous while logged in as the user that should be the running user of the schedulable class – cropredy Jan 6 '16 at 20:07
  • @mkorman Is there a way to schedule the job so that it deletes cases that are 30 days old in that queue? Right now it will delete everything on the last day of the month but what I would like is for it to delete everything that is 30 days old and leave everything that is less than 30 days. :) – cloudy-ritz Jan 7 '16 at 15:43
  • I would solve that inside the batch class. That is: I would still run the batch class every 15 days as we mentioned above, but inside the "start" method of the batch only select cases that are 30 days old. If you cannot do that easily in a SELECT query, you can select all cases, and inside the execute method only delete those that are old. If you need more help, we'd need to see the code of the batch class itself – mkorman Jan 7 '16 at 15:53

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.