In servicenow, we are using flow designer for the catalog request. Existing we have a ask for approval action in flow when the first task was closed complete and trigger the approval to a particular group. But our requirements has been changed and now i want to populate the approval to the previously closed task assignment group particular member. In previous task group assigned by assignment group and i have a list of all groups and a particular member from each group if there group is in previous task. If he approve then request go to next step and if reject then reject the state. How to do this via script
Assistant
User
How to get the sys id of previous created task
Assistant
In ServiceNow, you can utilize Flow Designer’s “Script” action to achieve the outlined requirement. Here are the steps to create your custom flow with a script to dynamically assign approvals:
Identify the script action: First, understand where you will put the script within the flow. In this case, after the task closure and before the approval action.
Fetch the Assignment Group from the Previous Task: Use a script to get the Assignment Group from the previous task, then determine the specific member who needs to approve.
Assign the Approval to the Specific User: Create a custom approval action which dynamically assigns the approval request.
Here’s a breakdown of how you might implement this:
Step-by-Step Script Implementation:
Create or Edit the Flow:
Navigate to “Flow Designer” and open your existing catalog request flow or create a new one.
Add an Action to Check Task Closure:
Make sure you have an action that checks if the first task was closed and completed.
Add a Script Action to Fetch Assignment Group:
Drag and drop a “Script” action to your flow where you need to handle this logic.
Here's a sample script for this part:
javascript
(function execute(inputs, outputs) {
// Fetch the previously closed task from context
var previousTaskSysId = 'sys_id_of_previous_task'; // You can source this from flow variables or context
var task = new GlideRecord('task');
if (task.get(previousTaskSysId)) {
var assignmentGroup = task.assignment_group;
// Now find the specific member from the assignment group
var groupMember = new GlideRecord('sys_user_grmember');
groupMember.addQuery('group', assignmentGroup);
groupMember.query();
// Assuming there's a predefined member to approve from each group
// e.g., 'approver' field in a custom table
var approver;
while (groupMember.next()) {
approver = groupMember.user; // Or fetch from a specific field used for approvers
break; // Assuming you are only looking for one member
}
// Populate the output to be used in the next step
outputs.approverSysId = approver;
}
})(inputs, outputs);
Add an Approval Action:
After the script action, add an “Ask for Approval” action.
For the “Approvers” field select the outputs from the previous script (i.e., outputs.approverSysId).
Handle Approval/Rejection:
Configure the flow to handle approvals and rejections, directing to the subsequent steps as required.
Add actions for approved state, such as proceeding to the next step.
Add actions for rejection, such as setting the state to rejected and notifying necessary parties.
Example Configuration for Approval Handling:
swift
// On Approval: Proceed to Next Step
// On Rejection: Set State as Rejected
Putting it all together:
Your flow should look something like this:
Trigger: Catalog Request
Check Task Closure: Condition (Task Closed and Completed)
Script to Fetch Approver:
Inputs: Sys_id of the previous task
Script Action: As shown above
Outputs: approverSysId
Ask for Approval:
Approvers: Use the output from the script (outputs.approverSysId)
Dynamic Branch Based on Approval:
On Approval: Proceed to next step
On Rejection: Set State as Rejected
This setup dynamically assigns the approval request to a specific individual from the previously assigned group, based on the closure of an initial task. Modify the script as per your custom logic and fields in ServiceNow.
To get the sys_id of the previously created task within a Flow in ServiceNow, you generally need to track or reference the task created initially.
Here's how you can get the sys_id of the previous task:
Store the sys_id: When the first task is created in the flow, store its sys_id in a Flow variable.
Reference the Flow variable: Use this Flow variable in subsequent actions to reference the initial task.
Here's the step-by-step approach:
Step-by-Step Implementation:
Create a Flow Variable:
Open your Flow Designer.
Create a new variable to store the sys_id of the first task, let's name it previousTaskSysId.
Store sys_id when Creating the Task:
When you create the initial task within the Flow, use a script to store its sys_id in the variable created.
Example:
Step 1: Create Variable
In Flow Designer, go to + New, create a new variable previousTaskSysId of type String.
Step 2: Create Task and Store sys_id
When creating a task (e.g., using the “Create Task” action), use an output to store the sys_id.
javascript
// Example of creating a task and storing its sys_id
var task = new GlideRecord('task');
task.initialize();
task.short_description = 'Initial Task';
task.insert();
// Store the sys_id in the flow variable
flowVars.previousTaskSysId = task.sys_id.toString();
Alternatively using Built-in Create Task Action:
If you are using the built-in “Create Task” action, ensure to capture the sys_id in the output and store it.
Step 3: Reference the sys_id in Subsequent Script
javascript
(function execute(inputs, outputs) {
// Fetch the previously created task sys_id from the flow variable
var previousTaskSysId = flowVars.previousTaskSysId; // Reference to the flow variable
if (previousTaskSysId) {
// Fetch task details
var task = new GlideRecord('task');
if (task.get(previousTaskSysId)) {
var assignmentGroup = task.assignment_group;
// Find the specific member from the assignment group
var groupMember = new GlideRecord('sys_user_grmember');
groupMember.addQuery('group', assignmentGroup);
groupMember.query();
var approver;
while (groupMember.next()) {
approver = groupMember.user; // Or fetch from a specific field used for approvers
break; // Assuming you are only looking for one member
}
// Populate the output to be used in the next step
outputs.approverSysId = approver.sys_id;
}
}
})(inputs, outputs);
Summary:
Here's a summarized Flow:
Trigger: Catalog Request.
Create First Task: Using “Create Task”.
Store sys_id in flow variable previousTaskSysId.
Check Task Closure: Condition (Task Closed and Completed).
Script to Fetch Approver:
Inputs: Reference previousTaskSysId.
Script Action: As shown above.
Outputs: approverSysId.
Ask for Approval: Use outputs.approverSysId.
Handle Approval/Rejection:
On Approval: Proceed to the next step.
On Rejection: Set state as rejected.
This approach ensures that the sys_id of the previously created task is stored and referenced correctly within your Flow Designer implementation. Modify the logic according to your specific table names and fields.