Filter Functionality in Reference field

rohit_murli
Tera Contributor

Hello All,

We have a requirement where, on the catalog form, if the Line of Business field value is selected, the SVP/VP reference field should filter and display the relevant names based on the selected value, allowing the user to choose from the options.

Currently, we have written a script include that displays the group members of the selected Line of Business in the reference field. However, in the client script, we are using the setValue method, which sets all member names to the field instead of providing a filter option for user selection.

To achieve the functionality where users can select from a filtered list, what changes should we make to the following client script?

 

 

var ga = new GlideAjax('JobGroupMember');
ga.addParam('sysparm_name', "getMembers");
ga.addParam('sysparm_organization', line);
ga.getXMLAnswer(callback);

function callback(response) {
var answer = JSON.parse(response); {
g_form.setValue("svp", answer.join(','));
}
}
}

 

Any help will be appreciated. 
Thank you.

1 ACCEPTED SOLUTION

@rohit_murli 

is group name variable or organization variable a string type or a variable

if it's string holding group name then update your script include function as this

 getMembers: function(groupName) {
     var users = [];
     var members = new GlideRecordSecure('sys_user_grmember');
     members.addQuery('group.name', groupName);
     members.query();
     while (members.next()) {
         var mates = members.user.sys_id.toString();
         users.push(mates);
     }
     return 'sys_idIN' + users.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

View solution in original post

13 REPLIES 13

@rohit_murli 

did you check script include got called correctly with the group name?

are there members in those groups?

 

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

Hello @rohit_murli 

  1. For reference field we do not set the value instead we filter the list.
  2. You do not need client script as the script include is called directly from the reference qualifier.
  3. Script include created should not be client callable. Create a new script include without client callable as simply unchecking the box will not work.

Here is the updated script in script include:

 

    getMembers: function(line) {
		if (line == 'Commercial') {
			line = 'Job Request - Commercial VP/SVP';
		} else if (line == 'Finance') {
			line = 'Job Request - Finance VP/SVP';
		}else if (line == 'Graphic Solutions') {
			line = 'Job Request - Graphic Solutions VP/SVP';
		}else if (line == 'HR') {
			line = 'Job Request - HR VP/SVP';
		}
        var users = [];
        var line = 'Snow Dev';
        var members = new GlideRecordSecure('sys_user_grmember');
        members.addQuery('group.name', line);
        members.query();
        while (members.next()) {
            users.push(members.getUniqueValue());
        };
        return "sys_idIN" + users.join();
    },

 

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

 

 

@rohit_murli 

your variable should refer to sys_user and not sys_user_grmember

You want users to be shown belonging to that group

So please update that and it should work fine

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

Juhi Poddar
Kilo Patron

Hello @rohit_murli 

Based on my understanding, the requirement is for the second field on the form, which is a reference field, to filter its list based on the input provided in the first field.

To achieve this, adding an advanced reference qualifier to the second field should be sufficient to meet the requirement.

 

javascript:field_on_reference_table=current.variables.line_of_business 

Adding screenshot for better understanding:

JuhiPoddar_0-1736155929586.png

Note: field_on_reference_table is the field on reference table which is equal to variable line of business.

 

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar