Workflow Auto-Approving after Setting Approver from JSON
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 05:54 PM
I am dynamically setting the approver of a workflow to the Instructor of a course. The Instructor is populating correctly as the Approver. However, the workflow continues to auto-approve, despite the Approver taking no action on the record.
// Retrieve the instructor from the workflow's scratchpad
var instructor = workflow.scratchpad.instructor; // Assuming the instructor's info is stored here
// Ensure the instructor is defined in the scratchpad
if (instructor) {
// Log the instructor's value for debugging
gs.info('Instructor from scratchpad: ' + instructor);
// Query the sys_user table to find the instructor
var instructorUser = new GlideRecord('sys_user');
// Check if the instructor value is an email, name, or sys_id
if (instructor.indexOf('@') !== -1) {
// If it looks like an email, search by the email field
instructorUser.addQuery('email', instructor);
} else {
// Otherwise, treat it as a name (you can change this if it's sys_id directly)
instructorUser.addQuery('name', instructor);
}
instructorUser.query();
if (instructorUser.next()) {
// Successfully found the user record for the instructor
gs.info('Instructor sys_id: ' + instructorUser.sys_id);
// Retrieve the task related to the requested item (or the current task in workflow context)
var task = current; // Assuming 'current' is the task record in the workflow context
if (task) {
// Set the instructor's sys_id as the approver
task.approver = instructorUser.sys_id;
// Now, we need to trigger the approval request
task.approval = 'requested'; // Set the approval field to "Requested"
task.state = 1; // State = "In Progress" (this is usually used for approval tasks)
// Save the task with the updated approver and approval request
task.update();
gs.info('Task approver set to instructor: ' + instructor);
// Prevent auto-completion of the task:
// Ensure that the task stays in an "In Progress" state and doesn't close automatically.
// This prevents workflow from processing task automatically.
task.state = 2; // State = "Waiting for Approval" (or another appropriate state)
task.update();
gs.info('Task state set to "Waiting for Approval" for instructor to review.');
// Make sure the task stays in a pending state (not auto-approved)
// Set approval state to 'requested' on the sysapproval_approver table
var approval = new GlideRecord('sysapproval_approver');
approval.initialize();
approval.approver = instructorUser.sys_id;
approval.sysapproval = task.sys_id; // Link approval to the task
approval.state = 'requested'; // Set approval state to "Requested"
approval.insert(); // Insert the approval record to trigger approval manually
gs.info('Approval requested for task with instructor as approver.');
// Optionally, check if the task has an approval activity
// If it's tied to an Approval - User record, make sure it's waiting for a response.
} else {
gs.error('No task found for the requested item.');
}
} else {
// Log an error if the instructor cannot be found
gs.error('Instructor not found in the sys_user table.');
}
} else {
// Log an error if the instructor value is not in the scratchpad
gs.error('Instructor was not found in the workflow scratchpad.');
}
Should I be placing a catalog task between the run script and approval - user to set the record to waiting for approval? Seems trivial, but I can't seem to figure it out.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 06:15 PM
You'r script looks OK but way of creating approval's are not correct using script.
You have to use approval user activity to trigger the approval to the user.
As you finding the instructor sys_id you can assign this to the workflow parameter and call it in approval activity like below:
//your code
if (instructorUser.next()) {
// Successfully found the user record for the instructor
gs.info('Instructor sys_id: ' + instructorUser.sys_id);
workflow.scratchpad.instructorapprovaluser = instructorUser.sys_id.toString();
//your code
//In approval user activity
//In script section try like below:
var answer = [];
answer.push(workflow.scratchpad.instructorapprovaluser);
Let me know if you still stuck
Murthy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 06:43 PM
@appstorm verify [approval] field value on your task where approves generated whether changing from requested to approved and are approvers in state [no longer required]?