Setting Approver Dynamically from Workflow Editor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2025 07:38 AM
I'm trying to dynamically set the approver using the workflow approval-user activity and the following script that pulls the instructor name from a reference field on the requested item.
var courseInfo = current.variables.course_information;
if (courseInfo) {
var gr = new GlideRecord('sn_uni_req_larf_banner_courses');
if (gr.get(courseInfo)) {
var instructorSysId = gr.getValue('u_instructor_name'); // Get sys_id, not display value
var userGR = new GlideRecord('sys_user');
if (userGR.get(instructorSysId) && userGR.active) {
workflow.scratchpad.instructorName = instructorSysId; // Store actual sys_id
current.approver = instructorSysId; // Set approver immediately
gs.info('[Run Script] Valid instructor sys_id stored in scratchpad and assigned as approver: ' + instructorSysId);
} else {
workflow.scratchpad.instructorName = '';
current.approver = ''; // Clear approver if user is inactive
gs.warn('[Run Script] Instructor user is inactive or not found: ' + instructorSysId);
}
} else {
workflow.scratchpad.instructorName = '';
current.approver = ''; // Clear approver if course info is not found
gs.warn('[Run Script] No matching course record found for courseInfo: ' + courseInfo);
}
} else {
workflow.scratchpad.instructorName = '';
current.approver = ''; // Clear approver if course_information is not set
gs.warn('[Run Script] course_information variable not set.');
}
Logs show an active user/ sys_id is found and assigned, but it is not assigning an approver and waiting for his/ her approval. Instead, it is auto-approving and essentially skipping this step.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2025 07:56 AM
you should set the answer variable with the approver sysId
If that variable is reference type and the field "u_instructor_name" within that table is reference to sys_user then simply use this
var courseInfo = current.variables.course_information;
if (courseInfo) {
answer.push(current.variables.course_information.u_instructor_name.toString());
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2025 08:57 AM
Thank you for the reply! Testing your code in the advance script section under the Approval - User action yielded this result:
"Route to Instructor for Approval(180e467f3b0d6690f467b18a25e45aa2): Illegal access to getter method getMessage in class org.mozilla.javascript.RhinoException"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2025 09:14 AM
Is my assumption correct?
If that variable is reference type and the field "u_instructor_name" within that table is reference to sys_user?
are you creating workflow in scoped app?
is that course table in scoped app?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2025 09:41 AM
Your assumption is correct: u_instructor_name is stored on a separate table that references the sys_user table to populate that field. Both tables are not in a scoped app.