To implement the logic where a workflow automatically proceeds with a 'Yes' after a 1-day waiting period without user response, using a Script Include in conjunction with a Scheduled Job can be an effective approach. Below, I outline the steps and provide an example script to meet your needs.
Step 1: Prepare Your Workflow
Ensure your workflow is set up correctly:
- The workflow should pause on a "Wait for Condition" activity, evaluating something like
answer == true (assuming answer is the field being updated by the user).
- Have a mechanism (like a datetime field on the associated record) to track when the "Wait for Condition" activity starts waiting for user input.
Step 2: Create a Script Include
A Script Include in ServiceNow is a reusable JavaScript code that can be called from anywhere within your ServiceNow instance.
- Navigate to System Definition > Script Includes.
- Click New to create a new Script Include.
- Set the name (e.g.,
AutoAdvanceWorkflow) and ensure it's accessible (Active is true).
- In the Script field, insert your custom logic to find workflows waiting for the user response beyond the 1-day threshold and to update them as necessary. Here's an example script:
var AutoAdvanceWorkflow = Class.create();
AutoAdvanceWorkflow.prototype = {
initialize: function() {
},
autoAdvanceDueWorkflows: function() {
var gr = new GlideRecord('your_table_name'); // Replace 'your_table_name' with the name of your table
gr.addQuery('your_date_time_field', '<', 'javascript:gs.daysAgo(1)'); // Replace 'your_date_time_field' with your datetime field name
gr.addQuery('your_confirmation_field', '!=', true); // assuming 'your_confirmation_field' is waiting for a boolean 'true' as confirmation
gr.query();
while (gr.next()) {
// Update the record to simulate the 'Yes' response
gr.your_confirmation_field = true; // Set the appropriate field to simulate 'Yes'
gr.update();
// Optionally, add code to programmatically advance the workflow or notify the user, etc.
}
}
};
This code assumes:
- You have a DateTime field tracking when the workflow step began waiting.
- A 'confirmation' field indicates the user's response.
Step 3: Create a Scheduled Job
- Navigate to System Definition > Scheduled Jobs.
- Click New to create a Scheduled Job. Choose to define the job
Without using a template.
- Set a name (e.g., "Auto-Advance Workflows") and a schedule (running once every hour or once daily, depending on your preference).
- In the Script field, call the Script Include you made earlier:
new AutoAdvanceWorkflow().autoAdvanceDueWorkflows();
Step 4: Test Your Implementation
Thoroughly test this logic in a development or testing instance before deploying it to production. Make adjustments as necessary to ensure it accurately identifies and updates the correct records, and that it triggers under the right circumstances.
Important Considerations
- Customization: Adjust field names and logic according to your specific implementation details and requirements.
- Consistency: Ensure the datetime format and timezone in your comparisons are consistent and account for any necessary offsets or conversions.
- Notifications: You may want to add functionality within the Script Include or as a separate action in your workflow to send a notification to the user when their task is auto-advanced.
This approach allows you to programmatically handle the automatic progression of workflows based on time elapsed without a user response, fitting your scenario of a default 'Yes' after 1 day.