How to trigger approval through Script Include?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2023 04:29 AM - edited ‎09-15-2023 04:14 AM
I have a scenario where the approvals are triggered but the Approver User left the organization without approving the generated approvals. Now, I have to re-assign those approvals to new user. Please provide me with an approach to build functionality for business users using which they can select the approval records and reassign the records to new users.
I tried one approach by creating UI Action of List Choice type on Approval table [sysapproval_approver], creating UI Page and Script Include. However, the following functionality is not able to update the existing approvals and is not able to create complete new approval records. It creates new approvals with only one field value i.e. Approver and all other fields remain empty.
1. UI Action "Re-assign Approver" : After user selects the approval records to be reassigned, he can click on "Re-assign Approver" List Choice type UI Action. It will collect the Sys IDs of all the selected Approval records using g_list.getChecked() and will populate a UI Page where user can select new Approver user.
2. UI Page "Re-assign Approver" : UI page will allow user to select new approver user to whom the approval records should be re-assigned to. On Submit, the Sys IDs of all the selected approval records and the Sys ID of new approver user will be sent to Script Include so that the approval records can be assigned to new approver.
3. Script include :
var ReAssignApprover = Class.create();
ReAssignApprover .prototype = Object.extendsObject(AbstractAjaxProcessor, /** @lends global.module:sys_script_include.ReAssignApprover .prototype */ {
reAssignApprover: function() {
var findingsIDs = this.getParameter("sysparm_findings_ids");
var approverID = this.getParameter("sysparm_approver_id");
approverID = approverID.toString();
findingsIDs = findingsIDs.toString();
if (approverID == '') {
gs.addErrorMessage('Please select Approver.');
return false;
} else if (approverID != '') {
var approvals = new GlideRecord('sysapproval_approver');
approvals.addQuery('sys_id','IN',findingsIDs);
approvals.query();
while(approvals.hasNext()){
approvals.state = 'not_required';
approvals.update();
var newApproval = new GlideRecord('sysapproval_approver');
newApproval.initialize();
newApproval.setValue('state', 'requested');
newApproval.setValue('approver', approverID);
newApproval.document_id = approvals.document_id;
newApproval.due_date = approvals.due_date;
newApproval.expected_start = approvals.expected_start;
newApproval.group = approvals.group;
newApproval.process_step = approvals.process_step;
newApproval.risk_assessment_instance_approval = approvals.risk_assessment_instance_approval;
newApproval.source_table = approvals.source_table;
newApproval.state_binding = approvals.state_binding;
newApproval.sysapproval = approvals.sysapproval;
newApproval.u_escalation_count = approvals.u_escalation_count;
newApproval.insert();
}
return true;
}
},
type: 'EPS_ReAssignFindingsApprover'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2023 04:34 AM - edited ‎08-31-2023 04:39 AM
Hello @Novin Ramteke ,
you have write a fix script which glides sysapproval_aprrover table and take out the records where that left user is approver and update the approver to the new user
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('approver', 'sys_id_of_old_user');
gr.addQuery('state','requested');
gr.query();
while(gr.next())
{
gr.approver="new_user_sys_id";
gr.update();
}
Hope this helps
Mark the answer correct if this helps you
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2023 04:38 AM
Hello @Novin Ramteke,
One approach is to build functionality to reassign the existing approvals to new user (his/her manager) in ServiceNow is to use a script to update the approver field of the approval records. For example, you can create a script that takes the old user and the new user as parameters, and then queries the approval table for records where the approver is the old user and the state is requested. Then, you can loop through the results and update the approver field to the new user. You can also add some logic to send notifications or comments to inform the new approver of the change. Here is a sample script that illustrates this idea:
// Define the old and new users
var oldUser = 'old.user@company.com';
var newUser = 'new.user@company.com'; //if suits your use case, use manager's email id
// Query the approval table for records where the approver is the old user and the state is requested
var approvalGR = new GlideRecord('sysapproval_approver');
approvalGR.addQuery('approver.email', oldUser);
approvalGR.addQuery('state', 'requested');
approvalGR.query();
// Loop through the results and update the approver field to the new user
while (approvalGR.next()) {
approvalGR.approver = newUser;
approvalGR.update();
// Add some logic to send notifications or comments to inform the new approver of the change
// For example, you can use gs.eventQueue() or gs.addInfoMessage()
}
You can run this script as a background script or as a scheduled job, depending on your requirements. You can also modify it to suit your specific needs.
Hope this help.
Kind Regards,
Swarnadeep Nandy