Setting Approver Dynamically from Workflow Editor

appstorm
Tera Contributor

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. 

 

Screenshot 2025-04-28 at 10.35.30 AM.png

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.

 

Screenshot 2025-04-28 at 10.32.58 AM.png

 

 

Screenshot 2025-04-28 at 10.31.26 AM.png

 

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@appstorm 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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"

@appstorm 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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.