Wait for condition - is not proceeding with the work flow

Lavi Buchnik
Giga Contributor

Hi,

I'm working on a work flow which basically do this:

- Create a change

- Wait for condition, waits till change status is "implemented" (manually by a user)

- Once "implemented" should run a task 

The problem is that "wait for condition" script is getting the right value (answer = true) but not proceeding to the task.

Wait for condition script is this:

// This script check for if the change was implemented
answer = ifScript();

function ifScript() {
    var gr = new GlideRecord('change_request');
    gr.addQuery('number', workflow.scratchpad.chgNumber);
    gr.query();
    while (gr.next()) {
        if (gr.state == -1) {
            answer = true;
        } else {
            answer = false;
        }
    }
}

Tracing for the work flow and code, "wait for condition" is finished to run. the code gets to "answer = true" line.

but not clear why the flow is not proceeding. 

Also, inside the "Wait for condition" "When to run" part, the "Condition" is "Standard" type and its value is: 

activity.result == 'true'

On documentation: https://docs.servicenow.com/bundle/paris-servicenow-platform/page/administer/workflow-activities/reference/r_WaitForCondition.html#d2183351e150

It says this: 

States: The activity state tells the workflow engine what to do with the activity.

Which seems like what I'm missing, but not clear to me how to implement "States".

Thanks in advance,

Lavi

1 ACCEPTED SOLUTION

Hi Lavi,

I think the "Wait for condition" condition script is incorrect. Please modify it as below:

answer = ifScript();

function ifScript() {
    var chgGR = new GlideRecord('change_request');
    chgGR.addQuery('number', workflow.scratchpad.chgNumber);
    chgGR.query();
    if (chgGR.next()) {
        if (chgGR.getValue('state') == -1) {
            return true;
        } else {
            return false;
        }
    }
}

In your original code you are setting answer = true or false with in the if script, but the function does not return anything, so answer variable is getting set to undefined and the true value is overwritten.


Also please check if wait for condition, always condition is defined like this.
find_real_file.png

Let us try a different way of writing the business rule.

find_real_file.png

find_real_file.png

(function executeRule(current, previous /*null when async*/ ) {
    var wkfw = new Workflow();
    var context = wkfw.getContexts(current.parent);
    while (context.next()) {
        if (context.getValue('state') == 'executing') {
            new Workflow().broadcastEvent(context.getValue('sys_id'), 'update');
        }
    }
})(current, previous);

 

Hopefully this helps!

 

--

Thanks and regards,

Subrahmanyam

View solution in original post

11 REPLIES 11

Hi Lavi,

I think the "Wait for condition" condition script is incorrect. Please modify it as below:

answer = ifScript();

function ifScript() {
    var chgGR = new GlideRecord('change_request');
    chgGR.addQuery('number', workflow.scratchpad.chgNumber);
    chgGR.query();
    if (chgGR.next()) {
        if (chgGR.getValue('state') == -1) {
            return true;
        } else {
            return false;
        }
    }
}

In your original code you are setting answer = true or false with in the if script, but the function does not return anything, so answer variable is getting set to undefined and the true value is overwritten.


Also please check if wait for condition, always condition is defined like this.
find_real_file.png

Let us try a different way of writing the business rule.

find_real_file.png

find_real_file.png

(function executeRule(current, previous /*null when async*/ ) {
    var wkfw = new Workflow();
    var context = wkfw.getContexts(current.parent);
    while (context.next()) {
        if (context.getValue('state') == 'executing') {
            new Workflow().broadcastEvent(context.getValue('sys_id'), 'update');
        }
    }
})(current, previous);

 

Hopefully this helps!

 

--

Thanks and regards,

Subrahmanyam

Hi Subrahmanyam,

 

Thanks a lot - It worked this time.

I just updated the "Wait for condition" as you suggested with the "return" option (instead of answer = true) and first tested it with that update only, and it finely did the job and workflow continue.

Here is my workflow trace status (finely):

find_real_file.png

WOW, Thanks a lot for your great help, appreciating it a lot!!!

Lavi