Help Changing Approver Email Field from Multi-line Text to Dropdown Menu

JGuerrero0323
Tera Expert

Hi everyone,

In my Catalog Item, I currently display approver email addresses in a multi-line text variable. This list is auto-populated based on the Business Unit selected by the user, and the script filters the correct approvers accordingly. The logic works fine.

However, I now need to change the setup. Instead of displaying all approvers in a multi-line text field, I need to show them in a dropdown menu so the user can select just one approver. The goal is to avoid sending approval requests to multiple people—only the selected approver should receive the request.

I'm currently using a Variable Set to handle the approver information. I'm not sure whether I should use a Lookup Select Box, Select Box, or a Reference variable type for this new dropdown. The variable name I want to use is dropdown_menu.

Could someone guide me on what I need to change in my current code setup (Script Include and Catalog Client Script) to make this work with a dropdown?

Here’s my current code:

Script Include: 

var getApprovalGroups = Class.create();
getApprovalGroups.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    // Get approval group based on the user's department
    getApprGroup: function () {
        var requestedForUserId = this.getParameter('sysparm_reqFor');

        // Get the user record based on sys_id
        var user = new GlideRecord('sys_user');
        user.get(requestedForUserId);

        // Get department name and remove spaces
        var departmentName = user.department.name;
        var departmentKey = departmentName.split(" ").join("");

        // Find a user group whose name ends with "<DepartmentKey>ApprovalGroup"
        var group = new GlideRecord('sys_user_group');
        group.addEncodedQuery('active=true^nameENDSWITH' + departmentKey + 'ApprovalGroup');
        group.query();

        if (group.next()) {
            return group.sys_id; // Return the group's sys_id
        }
    },

    // Get all group member emails except the current requester's email
    getEmailsOfGroupMembers: function () {
        var groupSysId = this.getParameter('sysparm_group');
        var emailToExclude = this.getParameter('sysparm_email');
        var emailList = [];

        var members = new GlideRecord('sys_user_grmember');
        members.addQuery('group', groupSysId);
        members.query();

        while (members.next()) {
            var memberEmail = members.user.email;
            if (emailToExclude != memberEmail) {
                emailList.push(memberEmail);
            }
        }

        return emailList.toString(); // Comma-separated list
    },

    // Get group member emails excluding both requester and exiting user
    getEmailsOfExitUserGroupMembers: function () {
        var groupSysId = this.getParameter('sysparm_group');
        var requesterEmail = this.getParameter('sysparm_email');
        var exitingUserEmail = this.getParameter('sysparm_exitEmail');
        var emailList = [];

        var members = new GlideRecord('sys_user_grmember');
        members.addQuery('group', groupSysId);
        members.query();

        while (members.next()) {
            var memberEmail = members.user.email;
            if (requesterEmail != memberEmail && exitingUserEmail != memberEmail) {
                emailList.push(memberEmail);
            }
        }

        return emailList.toString(); // Comma-separated list
    },

    // Get approval group for internal transfer based on business unit
    getApprovalGroupForInternalTransfer: function () {
        var newBusinessUnit = this.getParameter('sysparm_newBusinessUnit');

        // Remove spaces from the business unit name
        var businessUnitKey = newBusinessUnit.split(" ").join("");

        // Find matching group
        var group = new GlideRecord('sys_user_group');
        group.addEncodedQuery('active=true^nameENDSWITH' + businessUnitKey + 'ApprovalGroup');
        group.query();

        if (group.next()) {
            return group.sys_id;
        }
    },

    // Define the Script Include name
    type: 'getApprovalGroups'
});

Catalog Client Script: 

function onChange(control, oldValue, newValue, isLoading) {
    if (newValue == '') {
        g_form.clearValue('approver_email');
        return;
    }

    //Type appropriate comment here, and begin script below
    g_form.clearValue('approver_email');
    var exitUser = false;
    var catItem = g_form.getValue('catalog_item');
    if (catItem != '9d86533a836b5610145417447daad34c') { //catalog item is not exit user
        var email = g_form.getValue('email_address'); //variable on user information variable set
        var approvalGroup = new GlideAjax("global.getApprovalGroups");
        approvalGroup.addParam("sysparm_name", "getEmailsOfGroupMembers");
        approvalGroup.addParam("sysparm_group", newValue);
        approvalGroup.addParam("sysparm_email", email);
        approvalGroup.getXML(setApprovalgroup);
    } else {
        exitUser = true;
        var exitEmail = g_form.getValue('exiting_user_email');
        var email = g_form.getValue('email_address');
        var approvalGroup = new GlideAjax("global.getApprovalGroups");
        approvalGroup.addParam("sysparm_name", "getEmailsOfExitUserGroupMembers");
        approvalGroup.addParam("sysparm_group", newValue);
        approvalGroup.addParam("sysparm_exitEmail", exitEmail);
        approvalGroup.addParam("sysparm_email", email);
        approvalGroup.getXML(setApprovalgroup);
    }

    function setApprovalgroup(response) {
        var group = response.responseXML.documentElement.getAttribute("answer");
        if (group) {
            var list = group.split(',');
            //alert("pal item length:" + list.length);
            var mail = [];
            var resp = '';
            for (var i = 0; i < list.length; i++) {
                resp = list[i].split('\n');
                mail.push(resp[0]);
            }
            g_form.setValue('approver_email', mail.join('\n'));

        }
    }
}

I appreciate any tips or examples to help me convert this to a dropdown menu. Also, since I'm no longer using the multi-line text, I don't need to separate the emails by commas.

JGuerrero0323_0-1744162278621.png

 

Thanks in advance!

2 ACCEPTED SOLUTIONS

Shivalika
Mega Sage

Hello @JGuerrero0323 

 

If you want a drop-down of approvers - you cns just use Look Up select Box - this will act as both reference and dropdown. 

 

You will be able to select the table , the group and add all the conditions you want and then get the dropdown of users. 

 

As per your script I can understand that you are checking the group members and adding their mails in the mailing list. If the requestor mail is there you are ignoring that and if exiting user mail is there you are ignoring that. 

 

You can directly add that condition in the sys_user_grmember table, copy the query and add that as reference qualifier. 

 

OR 

 

Use this same script - remove the catalog script , just return the sysIDs of the users who you want as approver, that will be your reference qualifier. So automatically you will only be getting that in drop-down. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

View solution in original post

Robert H
Mega Sage

Hello @JGuerrero0323 ,

 

You can achieve this with a Lookup Select Box and without any scripting.

 

Assumptions from looking at your code:

  • The "Business unit" field refers to the Group [sys_user_group] table.
  • The selectable approvers shall be the members of the selected "Business unit" group.
  • You have an "Email address" variable and the group member with that email address shall not be part of the list of approvers.

Here is how you would need to configure the Lookup Select Box variable:

RobertH_0-1744183649552.png

 

Reference qualifier:

javascript&colon;'group='+current.variables.business_unit+'^user.email!='+current.variables.email_address+'^ORuser.emailISEMPTY'

Variable attributes:

ref_qual_elements=email_address;business_unit

Adjust these based on the actual names of your variables for Business Unit and Email Address.

Important: replace the "&colon;" in the reference qualifier with a ":" as shown in the screen shot. This gets added automatically when posting in this community.

 

Result:

 

Given that I have a group / business unit with these members:

RobertH_2-1744183821785.png

 

The lookup select box will show the following choices:

RobertH_1-1744183780260.png

 

I would recommend that you also create a UI Policy that hides this variable when the Email address or Business unit variables are empty.

 

Regards,

Robert

 

View solution in original post

6 REPLIES 6

Shivalika
Mega Sage

Hello @JGuerrero0323 

 

If you want a drop-down of approvers - you cns just use Look Up select Box - this will act as both reference and dropdown. 

 

You will be able to select the table , the group and add all the conditions you want and then get the dropdown of users. 

 

As per your script I can understand that you are checking the group members and adding their mails in the mailing list. If the requestor mail is there you are ignoring that and if exiting user mail is there you are ignoring that. 

 

You can directly add that condition in the sys_user_grmember table, copy the query and add that as reference qualifier. 

 

OR 

 

Use this same script - remove the catalog script , just return the sysIDs of the users who you want as approver, that will be your reference qualifier. So automatically you will only be getting that in drop-down. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Ankur Bawiskar
Tera Patron
Tera Patron

@JGuerrero0323 

you can use Reference variable to sys_user and then add advanced ref qualifier on it to show only the correct approvers based on Business unit selected

Then you can use this Reference variable in your flow or workflow to send approvals

You will have to tweak your script include so that it works based on advanced ref qualifier.

I hope you will be able to do that from your side.

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

Robert H
Mega Sage

Hello @JGuerrero0323 ,

 

You can achieve this with a Lookup Select Box and without any scripting.

 

Assumptions from looking at your code:

  • The "Business unit" field refers to the Group [sys_user_group] table.
  • The selectable approvers shall be the members of the selected "Business unit" group.
  • You have an "Email address" variable and the group member with that email address shall not be part of the list of approvers.

Here is how you would need to configure the Lookup Select Box variable:

RobertH_0-1744183649552.png

 

Reference qualifier:

javascript&colon;'group='+current.variables.business_unit+'^user.email!='+current.variables.email_address+'^ORuser.emailISEMPTY'

Variable attributes:

ref_qual_elements=email_address;business_unit

Adjust these based on the actual names of your variables for Business Unit and Email Address.

Important: replace the "&colon;" in the reference qualifier with a ":" as shown in the screen shot. This gets added automatically when posting in this community.

 

Result:

 

Given that I have a group / business unit with these members:

RobertH_2-1744183821785.png

 

The lookup select box will show the following choices:

RobertH_1-1744183780260.png

 

I would recommend that you also create a UI Policy that hides this variable when the Email address or Business unit variables are empty.

 

Regards,

Robert

 

Hello @JGuerrero0323 ,

 

So this simple and scriptless solution was not what you were looking for?

 

Regards,

Robert