3

I have a requirement to wait before calling a batch class from controller. can someone provide me some wait or poll kind of statements before I execute below lines? That will wait code execution to get all records where update happened in batch class.

 CallBatch M = new CallBatch ();
 Database.executeBatch(M); 
4
  • 2
    Are you waiting for something to happen before executing this or do you want to just wait for a certain amount of time? Say 10 minutes? – Lance Shi Sep 18 '15 at 6:27
  • I am confused, as the start you said wait to run the batch but then you say wait to gather the records updated in the batch. If the latter you can use action poller to call a method to check the status of the batch, if the former, you could use a combination of JS and action functions to wait to executed the method, but not sure what the use case would be and even then I am not sure the use case would be a valid one. – Eric Sep 18 '15 at 7:10
  • You can not achieve it using only Apex. Visualforce + Apex would work as suggested in above comments. Salesforce don't have wait (timeout) type methods yet. – Ashwani Sep 18 '15 at 9:02
  • Because a wait-timeout mechanism is not a smart solution. Let's try to think more high level and have a look at your entire design. What are you trying to achieve, and why or how have you chosen this approach ? – Samuel De Rycke Sep 21 '15 at 3:37
4

The Apex Language Utilities has a Stopwatch Class that may do what you're looking for. It can be used within Apex as a timer of sorts. The complete code for all of the utilities is available from a repository on Github. The code for the StopWatch Class is shown below.

/* ============================================================
 * This code is part of the 'apex-lang' open source project avaiable at:
 * 
 *      http://code.google.com/p/apex-lang/
 *
 * This code is licensed under the Apache License, Version 2.0.  You may obtain a 
 * copy of the License at:
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * ============================================================
 */
global class StopWatch {

    // running states
    private static final Integer STATE_UNSTARTED = 0;
    private static final Integer STATE_RUNNING = 1;
    private static final Integer STATE_STOPPED = 2;
    private static final Integer STATE_SUSPENDED = 3;

    // split state
    private static final Integer STATE_UNSPLIT = 10;
    private static final Integer STATE_SPLIT = 11;

    private Integer runningState = STATE_UNSTARTED;
    private Integer splitState = STATE_UNSPLIT;
    private Long startTime = -1;
    private Long stopTime = -1;

    global void start() {
        if (this.runningState == STATE_STOPPED) {
            throw new IllegalStateException('Stopwatch must be reset before being restarted. ');
        }
        if (this.runningState != STATE_UNSTARTED) {
            throw new IllegalStateException('Stopwatch already started. ');
        }
        this.stopTime = -1;
        this.startTime = System.currentTimeMillis();
        this.runningState = STATE_RUNNING;
    }

    global void stop() {
        if (this.runningState != STATE_RUNNING && this.runningState != STATE_SUSPENDED) {
            throw new IllegalStateException('Stopwatch is not running. ');
        }
        if (this.runningState == STATE_RUNNING) {
            this.stopTime = System.currentTimeMillis();
        }
        this.runningState = STATE_STOPPED;
    }

    global void reset() {
        this.runningState = STATE_UNSTARTED;
        this.splitState = STATE_UNSPLIT;
        this.startTime = -1;
        this.stopTime = -1;
    }

    global void split() {
        if (this.runningState != STATE_RUNNING) {
            throw new IllegalStateException('Stopwatch is not running. ');
        }
        this.stopTime = System.currentTimeMillis();
        this.splitState = STATE_SPLIT;
    }

    global void unsplit() {
        if (this.splitState != STATE_SPLIT) {
            throw new IllegalStateException('Stopwatch has not been split. ');
        }
        this.stopTime = -1;
        this.splitState = STATE_UNSPLIT;
    }

    global void suspend() {
        if (this.runningState != STATE_RUNNING) {
            throw new IllegalStateException('Stopwatch must be running to suspend. ');
        }
        this.stopTime = System.currentTimeMillis();
        this.runningState = STATE_SUSPENDED;
    }

    global void resume() {
        if (this.runningState != STATE_SUSPENDED) {
            throw new IllegalStateException('Stopwatch must be suspended to resume. ');
        }
        this.startTime += (System.currentTimeMillis() - this.stopTime);
        this.stopTime = -1;
        this.runningState = STATE_RUNNING;
    }

    global Long getTime() {
        if (this.runningState == STATE_STOPPED || this.runningState == STATE_SUSPENDED) {
            return this.stopTime - this.startTime;
        } else if (this.runningState == STATE_RUNNING) {
            return System.currentTimeMillis() - this.startTime;
        }
        return 0;
    }

    global Long getSplitTime() {
        if (this.splitState != STATE_SPLIT) {
            throw new IllegalStateException('Stopwatch must be split to get the split time. ');
        }
        return this.stopTime - this.startTime;
    }

    global Long getStartTime() {
        if (this.runningState == STATE_UNSTARTED) {
            throw new IllegalStateException('Stopwatch has not been started');
        }
        return this.startTime;
    }

    global String toStr() {
        return Datetime.newInstance(getTime()).format();
    }

    global String toSplitString() {
        return Datetime.newInstance(getSplitTime()).format();
    }

}

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.