Advanced reference Qualifier in ServiceNow based on other choice field.

surendra Gelivi
Tera Contributor

I have a requirement like their is two fields one is choice and another is reference. based on choice field selection the reference field values should change.
example: choice field have A,B,C .  if A is selects the reference field should show only few options.

if B is selects the reference field should show only few other options.


i used advanced reference qualifier : new Reference_field_validate().getUsers(current.request_type);
script include like : 

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

    getUsers: function(request_type) {
       
        var reqType = request_type;
        var Equery = "";
        var gr = new GlideRecord('sys_user');
       
        if (reqType == 'A') {

            Equery = 'u_levelINSr Director,Managing Director,Partner,Principal,Income Partner';
           
        }
        gr.addEncodedQuery(Equery);
        gr.query();
        var users = [];
        while (gr.next()) {
            users.push(gr.getUniqueValue());
            // users.push(gr.getValue('sys_id'));
        }
        // return users.join(',');
        return "sys_idIN" + users;
    },

    type: 'Reference_field_validate'
});

Any one can help me regarding this.
1 ACCEPTED SOLUTION

Appanna M
Tera Guru

Hello @surendra Gelivi ,

There is a syntax error (current.variables.request_type you should pass a parameter) in your reference qualifier please correct it like below:

Syntax of advanced reference qualifier:- javascript: new Reference_field_validate().getUsers(current.variables.request_type);

 

I have created a similar case in my PDI it's working fine. Can you try applying the same. My case the filter condition is applying with title.

Note: Go and create a new script include and make sure check the client callable option. 

You can try update your function code like below once. 

 

 

getUsers: function(request_type) {
        var Equery;
		var users = [];
		gs.info("ReqType:"+request_type); // To check the value is coming or not
        if (request_type == 'A') {
            Equery = 'u_levelINSr Director,Managing Director,Partner,Principal,Income Partner'; // Check the backend value for level.
        }else{
			Equery = '';
		}
		var gr = new GlideRecord('sys_user');
        gr.addEncodedQuery(Equery);
        gr.query();
        while (gr.next()) {
            users.push(gr.sys_id.toString());
        }
        gs.info("User List:"+users);
        return 'sys_idIN' + users;
    },

 

 

Below are the ref screenshots:

Appanna_0-1711997547163.png

 

Appanna_1-1711997571029.pngAppanna_2-1711997620862.pngAppanna_3-1711997678137.pngAppanna_4-1711997707206.png

Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.

 

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

I would assume you are getting unexpected results.  If the reference field shows all of the records from the sys_user table regardless of the choice, then it's having trouble calling the Script Include, or with the GlideRecord - ignoring the encoded query.  Confirm that the reference field is on the sys_user table, and the choice field value (not the label) is exactly what you are using in your if statement.  Temporarily add some gs.addInfoMessage lines like this to confirm what is happening in the script: 

 

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

    getUsers: function(request_type) {
       
		var reqType = request_type;
		gs.addInfoMessage('SI running. Request type = ' + reqType);
        var Equery = "";
        var gr = new GlideRecord('sys_user');
        if (reqType == 'A') {
            Equery = 'u_levelINSr Director,Managing Director,Partner,Principal,Income Partner';
		}
        gr.addEncodedQuery(Equery);
        gr.query();
        var users = [];
        while (gr.next()) {
			gs.addInfoMessage('User found ' + gr.name);
            users.push(gr.getUniqueValue());
            // users.push(gr.getValue('sys_id'));
        }
        // return users.join(',');
        return "sys_idIN" + users.join(',');
    },
    type: 'Reference_field_validate'
});

You'll also want to manually filter a list view of users by the elements in your encoded query, then right-click on the breadcrumb and choose Copy Query to confirm that this is correct.

 

Your continued use of the word field, as opposed to variable, and no mention of a Catalog Item has lead me to believe that this is happening on a table form.  If in fact you are using this on a Catalog Item variable, and we were somehow supposed to know this, then add .variables prior to request_type on your reference qualifier:

BradBowman_0-1711999269340.png

 

Appanna M
Tera Guru

Hello @surendra Gelivi ,

There is a syntax error (current.variables.request_type you should pass a parameter) in your reference qualifier please correct it like below:

Syntax of advanced reference qualifier:- javascript: new Reference_field_validate().getUsers(current.variables.request_type);

 

I have created a similar case in my PDI it's working fine. Can you try applying the same. My case the filter condition is applying with title.

Note: Go and create a new script include and make sure check the client callable option. 

You can try update your function code like below once. 

 

 

getUsers: function(request_type) {
        var Equery;
		var users = [];
		gs.info("ReqType:"+request_type); // To check the value is coming or not
        if (request_type == 'A') {
            Equery = 'u_levelINSr Director,Managing Director,Partner,Principal,Income Partner'; // Check the backend value for level.
        }else{
			Equery = '';
		}
		var gr = new GlideRecord('sys_user');
        gr.addEncodedQuery(Equery);
        gr.query();
        while (gr.next()) {
            users.push(gr.sys_id.toString());
        }
        gs.info("User List:"+users);
        return 'sys_idIN' + users;
    },

 

 

Below are the ref screenshots:

Appanna_0-1711997547163.png

 

Appanna_1-1711997571029.pngAppanna_2-1711997620862.pngAppanna_3-1711997678137.pngAppanna_4-1711997707206.png

Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.

 

Hi @Appanna M 

Thanks for your response it was working good but it was not validating "if" conditions.

Hello @surendra Gelivi ,

It should work for sure. Can you print the value before checking once and see the back end choice value of the request type. Then try once.

 

Please Mark My Answer as Helpful and Accept as Solution, if you find this article helpful or resolves your issue.