8

This code works in an Execute Anonymous window, but not when run from a rest controller:

    @RestResource(urlMapping='/scheduler')
    global with sharing class SchedulerController {

    @HttpDelete
    global static String stopPusher(){
        List<CronTrigger> triggers = [SELECT CronTrigger.Id FROM CronTrigger WHERE State = 'WAITING'  and CronJobDetail.Name Like 'Ambition%' ];
        if(triggers.size() == 0){
            return 'no jobs running';
        }
        String id =  triggers[0].id;
        System.abortJob(id);
        System.debug(id);
        return 'aborted ' + id;
    }}

I get the following response from the controller:

[{"message":"System.StringException: entityId : U#224.3fffffff (CronJobDetail)\n\nClass.ambition.SchedulerController.stopPusher: line 11, column 1","errorCode":"APEX_ERROR"}] 500

What is going on here?

I am calling the endpoint with the following python code:

url = 'https://na15.salesforce.com/services/apexrest/ambition/scheduler'
headers = {'Authorization': 'Bearer ' + access_token}
r = requests.delete(url, headers=headers) 
print r.text
print r.status_code
4
  • 1
    I can execute that code snipper just fine, could it be that you've not included enough code ? Error also indicates some controller method.. – Samuel De Rycke Feb 18 '14 at 18:35
  • Yes, please can you share the entire signature and return type? Have you obfuscated the exception message? – bigassforce Feb 18 '14 at 18:53
  • I will update the code snippet to include the controller. I did edit the error, but didn't think that was important. Line 14 is the System.abortJob(id); call. – edgartheunready Feb 18 '14 at 18:57
  • 1
    Am I glad I stumbled upon this error, thanks for posting, it saved me some hours of searching – pjcarly Apr 2 '14 at 13:58
8

Welcome to the world of known issues :

https://success.salesforce.com/issues_view?id=a1p30000000SyhIAAS

3
  • 2
    Thanks. Maybe I will schedule a job to delete the other job as a way to work around this issue. In the mean time, this is how I feel right now: memes.at/faces/whyyy.jpg – edgartheunready Feb 18 '14 at 19:30
  • 4
    actually, I ended up using a custom setting that the job checks before it runs. If the settings says 'stop', then it does not execute, and unschedules itself; if it says 'run', then it runs normally. the rest endpoint now manipulates this custom setting instead of trying to abort the job. – edgartheunready Feb 18 '14 at 20:10
  • 1
    That's a sleek workaround. Post your comment as an answer and mark your answer as solved. – Rao Feb 18 '14 at 21:05
5

As rao said, there is a SF known issue preventing jobs from being aborted in a custom rest controller.

Here is a simplified version of my job's execute method:

    public void execute(SchedulableContext SC) {
        //configUtil.getState() retrieves the custom setting with the 'RUNNING'/'STOPPED' state information.
        if(configUtil.getState() != 'RUNNING'){
            //abort the current job, and return.
            System.abortJob( SC.getTriggerId() );
            return;
        }

        //do stuff
        coolMethodThatDoesCoolStuff();

        //reschedule job to run again in 1 minute
        Datetime sysTime = System.now().addSeconds(60);      
        String chronExpression = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();
        System.schedule( 'AmbitionPusherSelfSchedule' + sysTime, chronExpression, new Pusher() );
        //abort the current job instead of running again in an hour.
        System.abortJob( SC.getTriggerId() );
    }

and here is the code from the controller:

@HttpDelete
global static String stop(){
    configUtil.setState('STOPPED');
    return 'job stop requested';
}
1

As others have mentioned, this is triggered by a known issue.

A different workaround is to use SOAP instead of REST.

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.