
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
12-17-2023 10:53 PM - edited 12-19-2023 09:20 PM
Many people use 'Wait for condition' workflow activity but they are not aware when exactly wait for condition activity is evaluated. Beginners think that the ‘Wait for condition’ activity will wait for the condition/script to evaluate to true and our work flow will proceed further once the condition is passed. Most of the assumption doesn’t work because the activity is not evaluated.
- What is “Wait for Condition” workflow activity?
In the ServiceNow workflow the Wait for condition activity is used to pause the workflow from moving to the next step and wait at this activity until the current record matches the specified condition. Use this activity to pause a workflow indefinitely until a particular criteria is met by a record update and that criteria can be set by the condition builder for the current record. Or we can use a script to return true/false by evaluating criteria for current record or any other related records. Learn more about this on link https://docs.servicenow.com/bundle/vancouver-build-workflows/page/administer/workflow-activities/ref...
Examples:
- Pause the workflow until user fills some fields on form (or meet any condition like state=resolved, Resolution Notes is not empty etc.)
- Another example can be based on the values of another record. Pause a workflow after approval until the requested for users field is updated. (e.g. VIP=true).
- Confusion about when exactly the criteria is evaluated?
I have noticed many questions on the community related to wait for condition behavior. Most of the time people post like they have a script that evaluates to true based on the record value in another table but workflow is not moving forward. In some cases it works when we add Work notes on our ticket.
In the above example the script is evaluating conditions for the records present in another(Source) table but workflow is running on a different(current) table. As the wait for condition is evaluated every time the current record is updated. Even though the value changes in the source table there is no update made to the current record, so it will not evaluate the condition. As soon as any user updates anything on the current record the script is evaluated and workflow proceed.
Let's take an example:
A catalog item is created to update users as ‘VIP’ users in AD (Active Directory) and it will be sync to ServiceNow through integration on scheduled intervals.
The workflow is like below:
- User submits a request and after an approval a SCTASK will be assigned to the AD Team to update the user as ‘VIP’ in AD.
- AD team will update users VIP status and will close the task
- And workflow will wait for the same to reflect in ServiceNow. The Wait for condition script would be like below:
// Set the variable 'answer' to true or false to indicate if the condition has been met or not.
var usergr = new GlideRecord('sys_user');
if (usergr.get(current.variables.requested_for.toString())) {
if (usergr.vip == true)
answer = true;
}
The workflow will be waiting for users VIP status to be updated as true. But workflow will not know automatically whether it is updated or not. To trigger an evaluation script we have two options. Update current RITM record when user VIP status changes to true through BR or use Workflow().broadcastEventToCurrentsContexts() API to update workflow context.
Option 1: We need to create a BR on user table it should run when User record is updated and VIP changes to true. The script would be like below:
(function executeRule(current, previous /*null when async*/ ) {
updateWorkflowStatus();
function updateWorkflowStatus() {
var ritmGr = new GlideRecord('sc_req_item');
ritmGr.addQuery("requested_for", current.sys_id);
ritmGr.addQuery('cat_item','b7a120bb2f8ff150ae17debcf699b6ca');
ritmGr.addActiveQuery();
ritmGr.query();
if (ritmGr.next()) {
new Workflow().broadcastEventToCurrentsContexts(ritmGr, 'update', null);
}
}
})(current, previous);
Option 2: Create a BR on user table same as above and use below script to update the RITM record with work notes. The script would be like below:
(function executeRule(current, previous /*null when async*/ ) {
updateWorkflowStatus();
function updateWorkflowStatus() {
var ritmGr = new GlideRecord('sc_req_item');
ritmGr.addQuery("requested_for", current.sys_id);
ritmGr.addQuery('cat_item','b7a120bb2f8ff150ae17debcf699b6ca');
ritmGr.addActiveQuery();
ritmGr.query();
if (ritmGr.next()) {
ritmGr.work_notes = 'For '+current.getDisplayValue() +' VIP status updated.';
ritmGr.update();
}
}
})(current, previous);
To learn more about this check the below KB Article.
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0693292
Thanks,
Anil Lande
- 9,521 Views