In servicenow, my requirement is to trigger the approval based on previous task closed assignment group. if the previous task is complete and assignment group is Mumbai then approval goes to the particular member of that group. we have 25 groups of this type. how to do this , we have flow designer for this RITM process
Assistant
User
we need to do this approval for all 25 groups, can we do it via script in flow designer
Assistant
User
i used washington version of servicenow and not showing script action
Assistant
User
i have a flow designer only to do this , is any other method to do this
Assistant
User
Since updating a decision tree for 25 groups can be cumbersome directly within Flow Designer, consider maintaining approver mappings in a table and using them to keep approver details up to date, referencing these within each decision branch. how to do
Assistant
In ServiceNow, you can achieve your requirement using Flow Designer to create a flow that triggers an approval based on the assignment group of the previous task. Here's a step-by-step guide on how to set this up:
Create a new Flow:
Navigate to Flow Designer: Process Automation > Flow Designer.
Click on New to create a new flow.
Give your flow a name, e.g., "RITM Approval Based on Previous Task".
Trigger:
Choose a trigger for your flow. In this case, it could be when a task is updated.
Select Created or Updated trigger.
Set the table to which your trigger applies (for example, sc_task if you are tracking tasks in the Service Catalog).
Add a Condition to check if the task is closed:
Add an If condition to check if the task is closed.
Under Condition, select State.
Set the condition to State is Closed.
Get the Parent RITM:
Add an action to get the parent RITM of the closed task.
Select Action and choose Lookup Record.
Set the source to the sc_req_item table, with a filter sys_id equals current.request_item.
Check the Assignment Group of the Previous Task:
Add another lookup to find the previous task associated with the same RITM.
Select Action and choose Lookup Records.
Set the source to the sc_task table, with a filter request_item equals current.request_item and state is not Closed. Sort by sys_created_on descending, limit to 1.
Verify the Assignment Group:
Add another If condition to check if the assignment group of the previous task matches Mumbai.
Under Condition, select Assignment Group.
Set the condition to Assignment Group is Mumbai.
Trigger Approval:
If the condition is met, add an Action to create an approval.
Use the Create Record action.
Set the table to sysapproval_approver.
Fill in the necessary fields like Approver (select the particular member), Document Id (link to the current sc_req_item), etc.
Publish the Flow:
Review all your steps to ensure they are correct.
Click on the Publish button to make your flow active.
Additional Tips:
Groups & Members Management: To simplify, maintain a dictionary object or a table in ServiceNow that maps assignment groups to their respective approval members.
Fallback Scenarios: Consider what should happen if there are multiple previous tasks or none at all.
Performance: Ensure your lookup queries are efficient, especially when dealing with large datasets.
By following these steps, you can set up a flow in ServiceNow that automates the approval process based on the assignment group of the previous task. This setup leverages Flow Designer's capabilities to orchestrate the necessary checks and actions dynamically.
Certainly! If you have a large number of groups (25 in this case) and each requires a specific approver, using a script within a Flow Designer to handle the logic can be more efficient. Here's an approach using a Script Action within Flow Designer to dynamically assign the approver based on the assignment group of the previous task.
Step-by-Step Instructions:
Create the Flow:
Navigate to Flow Designer: Process Automation > Flow Designer.
Click New to create a new flow and give it a name, for example, "Dynamic Group-Based Approval".
Set Up the Trigger:
Add a Trigger for when a task is updated.
Choose Created or Updated and set the table to sc_task.
Add Condition to Check for Closed Status:
Use an If condition to verify if the task's state is closed.
Configure: State is Closed.
Lookup Parent RITM:
Add a Lookup Record action to get the parent RITM.
Set the table to sc_req_item and filter where sys_id equals current.request_item.
Get Previous Task's Assignment Group:
Add a Lookup Records action to find the previous task related to the same RITM.
Configure to fetch records from the sc_task table where request_item equals current.request_item and state is not Closed (sort by sys_created_on descending, limit to 1).
Script to Determine Approver:
Add a Script action to handle the dynamic approval assignment.
Write your server-side script to identify the appropriate approver based on the assignment group.
Here is an example script you might add to dynamically determine the approver based on the assignment group:
javascript
(function() {
var approvalGroupMap = {
'Mumbai': 'sys_user_mumbai_approver', // Replace with the sys_id of the approver
'New York': 'sys_user_newyork_approver',
'London': 'sys_user_london_approver',
// Add all required groups here
};
// Retrieve the previous task's assignment group
var previousTaskGroup = previousTask.assignment_group.name;
// Determine the corresponding approver
var approverId = approvalGroupMap[previousTaskGroup];
if (approverId) {
// Create Approval record
var approval = new GlideRecord('sysapproval_approver');
approval.initialize();
approval.approver = approverId;
approval.document_id = current.request_item; // Link the approval to the request item
approval.insert();
// Log for debugging
gs.info('Approval created for group: ' + previousTaskGroup + ' with approver: ' + approverId);
} else {
gs.error('No approver found for group: ' + previousTaskGroup);
}
})();
Completing the Flow:
Link Actions:
Ensure you link the Lookup Record action outputs to your Script action.
Set the previousTask variable in your script to reference the output of the previous task lookup.
Publish the Flow:
After you have completed writing the script and configured all the steps, click Publish to activate the flow.
Summary:
This script leverages a mapping object (approvalGroupMap) to map assignment groups to their corresponding approvers. By maintaining this mapping in the script, you can efficiently handle the approval logic for multiple groups without adding a large number of conditional branches in your flow.
This method keeps your flow streamlined and makes it easier to manage as you can update the script to handle any changes or additions to the groups and their approvers.
I understand that in the Washington release of ServiceNow, you might not have the option to directly add Script actions in Flow Designer. However, you can achieve similar functionality using a combination of Workflow and Business Rules with Script Includes.
Here's how you can approach this:
Step-by-Step Instructions Using Workflow and Business Rules
Create a Workflow for RITM Approval:
Go to Workflow Editor: Workflow > Workflow Editor.
Create a new Workflow.
Set the table to sc_req_item (Request Item).
Add a Condition to Check the Previous Task's State:
Add a Condition activity to check if the previous task is closed.
Use the Join condition to fetch the previous task attached to the same Request Item that is closed. You might use a Script Include method here.
Script to Determine Approver:
Create a Script Include:
Go to System Definition > Script Includes.
Create a new Script Include to map groups to approvers and handle the creation of the approval record.
Here’s an example of a Script Include:
javascript
var GroupApprovalUtility = Class.create();
GroupApprovalUtility.prototype = {
initialize: function() {},
getApproverForGroup: function(groupName) {
var approvers = {
'Mumbai': 'sys_user_mumbai_approver', // Replace with actual sys_id
'New York': 'sys_user_newyork_approver',
'London': 'sys_user_london_approver'
// Add more groups and approvers mapping
};
return approvers[groupName] || null;
},
createApprovalRecord: function(requestItemSysId, groupId) {
var approverId = this.getApproverForGroup(groupId);
if (!approverId) {
gs.error('No approver found for group: ' + groupId);
return;
}
var approval = new GlideRecord('sysapproval_approver');
approval.initialize();
approval.approver = approverId;
approval.document_id = requestItemSysId;
approval.insert();
gs.info('Approval created for group: ' + groupId + ' with approver: ' + approverId);
},
type: 'GroupApprovalUtility'
};
Invoke the Script in Workflow:
Add a Run Script activity in your Workflow.
Use the GroupApprovalUtility Script Include to determine the approver and create the approval record.
Example script for Run Script activity:
javascript
// Fetch the previous task's assignment group (replace logic as required)
var previousTask = new GlideRecord('sc_task');
previousTask.addQuery('request_item', current.sys_id);
previousTask.orderByDesc('sys_created_on');
previousTask.query();
if (previousTask.next()) {
var groupId = previousTask.assignment_group;
var groupName = previousTask.assignment_group.name;
// Use the Script Include to create the approval
var utility = new GroupApprovalUtility();
utility.createApprovalRecord(current.sys_id, groupName);
}
Trigger the Workflow:
Ensure the Workflow runs when the RITM changes state or based on your specific condition.
You may set this in the Workflow properties or through a Business Rule.
Create a Business Rule (if needed):
If you need to trigger the Workflow from a specific Task state change, you can create a Business Rule.
Go to System Definition > Business Rules and create a new one:
Trigger it on update of sc_task.
Add a condition to check if the task is closed.
In the script, start the Workflow for the parent RITM.
Example Business Rule Script:
javascript
if (current.state == 'Closed') {
var wf = new Workflow();
wf.startFlow('<Workflow_Sys_ID>', current.request_item, 'update');
}
Summary:
Using Workflow and Script Include, you can create the logic to dynamically determine the approver based on the assignment group of the previous task. This method does not require Script actions within Flow Designer, offering compatibility with older versions like Washington.
Ensure all possible groups and approvers are correctly mapped in your Script Include, and tailor the script as required for your specific implementation and business logic.
Since you're required to use Flow Designer in the Washington release and the option for Script actions is not available, you can still achieve this by leveraging the capabilities within Flow Designer itself. Specifically, you can use sub-flows or additional actions available within Flow Designer to achieve your goal. Here’s an approach:
Setting Up the Flow in Flow Designer:
Create the Flow:
Navigate to Flow Designer: Process Automation > Flow Designer.
Click New to create a new flow and give it a name like "Dynamic Group-Based Approval".
Add Trigger:
Choose Created or Updated trigger.
Set the table to sc_task (Service Catalog Task).
Add a condition to only trigger when State is Closed.
Lookup Parent RITM:
Add a Lookup Record action to get the parent Request Item (RITM).
Set the table to sc_req_item and filter where the sys_id equals current.request_item.
Get Previous Task:
Add another Lookup Records action to find the previous task related to the same RITM.
Set the source to sc_task table, with a filter where request_item equals current.request_item and state is Closed. Sort by sys_created_on descending, limit to 1.
Add Decision for Each Group:
Add a Decision action branch for each Assignment Group. While this is more manual, it adheres to the constraints of not using script actions directly.
Approval Actions:
For each branch in the Decision action, add necessary approval tasks.
Use the Ask for Approval action to specify the approver in each case.
Example Breakdown:
Step-by-Step:
Trigger and Initial Conditions:
When: Record created or updated on sc_task
Field: State set to Closed
Lookup Parent RITM:
Action: Lookup Record
Table: sc_req_item
Conditions: sys_id equals current.request_item
Lookup Previous Task:
Action: Lookup Records
Table: sc_task
Conditions:
request_item equals current.request_item
state is Closed
Sort: sys_created_on descending
Limit: 1
Decision Nodes for Each Group:
Add a Decision action after retrieving the previous task.
Create a Decision Branch for each group you need to handle, e.g., "Is Mumbai Group?"
Condition in each branch:
Assignment Group equals Mumbai for the Mumbai branch.
Repeat for each group (New York, London, etc.).
Ask for Approval:
Under each Decision branch, add an Ask for Approval action.
For the Mumbai branch:
Approvers: Select the specific user or use a data pill to dynamically determine the user.
Approval Table: Set to sc_req_item or whatever is necessary.
Repeat for each branch according to the group's required approver.
Illustration:
Trigger:Created or Updated Record on sc_task table.
Action:Lookup Record
Table: sc_req_item
Condition: sys_id is current.request_item
Action:Lookup Records
Table: sc_task
Condition:
request_item is current.request_item
state is Closed
Sort: sys_created_on descending
Limit to: 1
Decision Action:
Branch: If assignment_group is Mumbai
...Other groups...
Approval Actions under Each Decision Branch:
For each branch, add:
Action: Ask for Approval
Approvers: Specific user or Dynamic resolution for each group.
Table: sysapproval_approver
Document ID: Data pill referencing the current.request_item.
Mapping and Maintenance:
Since updating a decision tree for 25 groups can be cumbersome directly within Flow Designer, consider maintaining approver mappings in a table and using them to keep approver details up to date, referencing these within each decision branch.
Summary:
This flow setup within Flow Designer is modular, using conditional logic to route to appropriate approval actions based on the assignment group. It's ideal since scripting directly within Flow Designer might be restricted in the Washington release, hence employing the decision node branch approach serves as a feasible alternative.
To streamline the process and avoid a cumbersome decision tree in Flow Designer, you can maintain approver mappings in a custom table. This approach involves creating a table to store the mappings of assignment groups to approvers and then leveraging this table within your flow. Here's how you can do this:
Step-by-Step Guide:
Create a Custom Table for Approver Mappings:
Go to System Definition > Tables.
Click on New to create a new table.
Provide a name for your table, such as GroupApproverMapping.
Add columns to the table: Assignment Group (Reference to sys_user_group) and Approver (Reference to sys_user).
Populate the Approver Mapping Table:
Navigate to your newly created table (GroupApproverMapping).
Add records for each of your groups, specifying the appropriate approver for each group.
Create a Script Include to Fetch the Approver:
Go to System Definition > Script Includes.
Create a new Script Include that fetches the approver based on the assignment group.
Example Script Include:
javascript
var GroupApproverUtil = Class.create();
GroupApproverUtil.prototype = {
initialize: function() {},
getApproverForGroup: function(groupId) {
var approver = null;
var gr = new GlideRecord('u_group_approver_mapping'); // Use your custom table's name
gr.addQuery('u_assignment_group', groupId);
gr.query();
if (gr.next()) {
approver = gr.u_approver;
}
return approver;
},
type: 'GroupApproverUtil'
};
Configure Flow Designer to Use Script Include:
Since Flow Designer does not directly support scripts in older versions like Washington, we can use a workaround using a Flow Action and a Business Rule to call the Script Include.
Alternative Approach Using Scripted REST API (Workaround):
Create Scripted REST API to Call Script Include:
Go to System Web Services > Scripted REST APIs.
Create a new Scripted REST API to act as an intermediate callable from Flow Designer.
Example Scripted REST API Script:
javascript
(function process(request, response) {
var groupId = request.queryParams.groupId;
var approver = new GroupApproverUtil().getApproverForGroup(groupId);
// Return the approver in the response
response.setBody(JSON.stringify({ approver: approver }));
})(request, response);
Define the API to accept a groupId parameter and return the approver's sys_id.
Call Scripted REST API from Flow Designer:
Create your flow in Flow Designer.
After fetching the previous task's assignment group, use the REST Step to call your Scripted REST API.
Steps in Flow Designer:
Trigger: Created or Updated on sc_task table where State is Closed.
Lookup Record: Fetch the parent sc_req_item.
Lookup Records: Fetch previous sc_task with the same request_item sorted by sys_created_on descending, limited to 1.
REST Step: To call your Scripted REST API.
Method: GET
URL: /api/now/table/{your_scripted_rest_api} with query parameter groupId from the previous task's assignment group.
Decision Action: Check if the approver returned from the API call is not empty.
Ask for Approval: Based on the approver returned from the API.
Parse the output and get approver from the JSON response.
Flow Designer Workflow:
Trigger: On Created or Updated for sc_task table when State is Closed.
Lookups:
Lookup Parent RITM (sc_req_item).
Lookup Previous Task (sc_task).
REST Call:
Call the Scripted REST API with the assignment group to obtain the approver (HTTP Method: GET).
Decision:
If approver is not null, proceed to the approval step.
Approval Action:
Ask for Approval: Use the approver value returned from the Scripted REST API.
Summary:
By maintaining a custom table to store group-to-approver mappings, creating a Script Include to fetch the appropriate approver based on the group, and utilizing a Scripted REST API to bridge the script capabilities with Flow Designer, you can efficiently manage dynamic approvers without cluttering your flow with numerous decision branches. This approach ensures scalability and ease of maintenance in a Flow Designer context.