Based on choice field value filter the data in reference field

gillerlaraj
Tera Contributor

HI There,

 

in catalog form based on choice field i need to restrcit the data in the referecne field based on choice field.

gillerlaraj_0-1736341801656.png

 

 

i written this script but it's not working any one help me what change i need to do in the code.

 

script include

-------------------

computerTest: function() {
        gs.log("inside function");
        var gr = new GlideRecord('cmdb_ci_computer');
        gr.addQuery('manufacturer.name',this.getParamter('sysparm_value'));
        gr.query();
        var a = [];
        while (gr.next()) {
            var computer = gr.getValue('sys_id');
            a.push(computer);
        }
        return "sysidIN" + a;
    },
 

client script (onchange- manufacturer)

---------------------------

var manufacturer = g_form.getValue('manufacturer');
    alert('manufacture is -->' + manufacturer);

    var gj = new GlideAjax("rajesh");
    gj.addParam('sysparm_name', 'computerTest');
    gj.addParam('sysparm_value', manufacturer);


    gj.getXMLAnswer(callback);

    function callback(result) {
        alert("Result " + result);
        // g_form.setValue( 'computer',result);
    }
 
gillerlaraj_1-1736344824064.png

 

 

 

 

 

Regards,

Rajesh

3 REPLIES 3

Juhi Poddar
Kilo Patron

Hello @gillerlaraj 

The script include needs a minor change. 

Script Include: 

computerTest: function() {
        gs.log("inside function");
        var gr = new GlideRecord('cmdb_ci_computer');
        gr.addQuery('manufacturer.name',this.getParamter('sysparm_value'));
        gr.query();
        var a = [];
        while (gr.next()) {
            var computer = gr.getValue('sys_id');
            a.push(computer);
        }
        return "sysidIN" + a.join(',');
    },

Note: Make sure script include is not client callable as we are calling it directly from reference qualifier.

Deactivate the onChange client script as this is not required.

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

JenniferRah
Mega Sage

You don't need the script. In your Computer variable, add a reference qualifier like this: 

javascript:'manufacturer.name='+current.variables.manufacturer;

 

It should look like this:

JenniferRah_0-1736346009550.png

 

Mahathi
Mega Sage
Mega Sage

Hi @gillerlaraj ,

Check this article and follow steps to fulfill your requirement.

https://www.servicenow.com/community/itsm-articles/dynamic-reference-field-values-based-on-select-bo...

If my answer helped in any way, please mark it as Correct & 👍Helpful

Regards,
Mahathi