- 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-23-2022 08:12 AM
Hi Lavi,
Which table is your workflow running against?
The workflow will only move to the next section once an update happens on the table that is running the workflow, so if a user manually changes the change status to 'implemented' on the change_request table, but your workflow is running on a different table then it will be stuck at the wait condition. If you manually made a change on the record that the workflow is running on (e.g. update the short description) once the condition is true, it would likely move to the next stage.
we had a similar scenario when we created a wait for condition on workflows running on the sc_req_item table that were to wait until all sc_tasks linked to the ritm were closed (including any that were not created by the workflow itself). All tasks would close, but the workflow was stuck at the wait condition, so we created a business rule to run on closure of the sc_task to force an update on the RITM:
(function executeRule(current, previous /*null when async*/ ) {
// force update of RITM when catalog task is closed.
var grRitm = new GlideRecord('sc_req_item');
grRitm.get('sys_id', current.request_item);
grRitm.setForceUpdate(true);
grRitm.update();
})(current, previous);
Hope this helps.
Thanks
Sam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-23-2022 09:35 AM
Hi Sam,
Thanks for the quick reply.
From "Workflow properties" the table name is: "sc_req_item".
Seems like what you described in exactly what I need. But not clear where should I put this code:
(function executeRule(current, previous /*null when async*/ ) {
// force update of RITM when catalog task is closed.
var grRitm = new GlideRecord('sc_req_item');
grRitm.get('sys_id', current.request_item);
grRitm.setForceUpdate(true);
grRitm.update();
})(current, previous);
Should it be in "wait for condition" just after my function call?
Thanks,
Lavi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-23-2022 09:46 AM
Hi Lavi,
The example code above is what we have in a business rule. Ours was a BR on the sc_task table as that is the table that our wait condition was focused on.
I would imagine that you would need the BR on your change_request table as that is the table that your wait condition is focused on. You could have the BR only trigger when the change status changes to 'implemented'. You would need to amend the get part of the script based on how the RITM is linked to your change request.
Hope this helps
Thanks
Sam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā03-23-2022 09:51 AM
Thanks again, will check it out and update with the results when done.