- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-23-2022 08:03 AM
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
Solved! Go to Solution.
- Labels:
-
Automated Test Framework
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-29-2022 06:05 AM
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.
Let us try a different way of writing the business rule.
(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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-29-2022 06:05 AM
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.
Let us try a different way of writing the business rule.
(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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-29-2022 06:37 AM
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):
WOW, Thanks a lot for your great help, appreciating it a lot!!!
Lavi