How to show reference field values based on selection of the record

Gopal14
Tera Contributor

Hi Team,

 

I am having 2 reference Variables.

1. Serial Number (alm_hardware table)

2. Hardware item (cmdb_hardware_product_model table)

 

In serial number if I select any record for that record we have model category. Let take I have selected 'Lenovo' for this record model category is "personal computer". 

In Hardware Item Variable, I need to show on "personal computer" model category only.

1 ACCEPTED SOLUTION

Hi @Gopal14 ,

Your client script seems to be having issue, please try the below one :

Client script:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue === '') {

        return;

    }

    var ga = new GlideAjax('GetProductModelBySerial');

    ga.addParam('sysparm_name', 'getProductModel');

    ga.addParam('sysparm_serial_number', newValue);
    ga.getXML(getProductModelParse);

    function GetDFcabParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        console.log(answer);
        g_form.setValue('hardware_item_variable', answer);

    }

}

Script Include:
Use the same one which you have created.

You can check whether you are getting the value in console of the browser which can be accessed by pressing Ctrl+shift+i and clicking console tab, search for the model name to see if it printed it or not.

Please mark my answer "Helpful" and "correct" if you feel that it has helped you in any way.

Thanks and Regards,
K. Sai Charan
Sr. ServiceNow Developer
Deloitte India

View solution in original post

15 REPLIES 15

Sai_Charan_K
Kilo Sage

Hi @Gopal14 ,

All you need to do is create an On change of client script based on serial number field where you will be passing this sys_id to the backend script include so that you can query the asset table and dotwalk to the product model and send it as answer to the client script so that you can set the values of the Hardware item variable.

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

K. Sai Charan

Sr. ServiceNow Developer

Deloitte India 

Hi @Sai_Charan_K 

 

Client Script:

 

function onChange(control, oldValue, newValue, isLoading){

if (isLoading || newValue === '') {

return;

}

 var ga = new GlideAjax('GetProductModelBySerial');

ga.addParam('sysparm_serial_number', newValue);

 ga.getXMLAnswer(function(response) {

var productModel = response;  

 g_form.setValue('hardware_item_variable', productModel);

});

}

 

 

Script Include:

 

var GetProductModelBySerial = Class.create();

GetProductModelBySerial.prototype = {

initialize: function() {},

getProductModel: function() {

var serialNumber = this.getParameter('sysparm_serial_number');

var assetGR = new GlideRecord('alm_asset');

 assetGR.addQuery('serial_number', serialNumber);

assetGR.query();

if (assetGR.next()) {  

return assetGR.model.getDisplayValue();

}

return '';

},

type: 'GetProductModelBySerial'

};

 

 

Not working above code,

Hi @Gopal14 ,

Your client script seems to be having issue, please try the below one :

Client script:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue === '') {

        return;

    }

    var ga = new GlideAjax('GetProductModelBySerial');

    ga.addParam('sysparm_name', 'getProductModel');

    ga.addParam('sysparm_serial_number', newValue);
    ga.getXML(getProductModelParse);

    function GetDFcabParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        console.log(answer);
        g_form.setValue('hardware_item_variable', answer);

    }

}

Script Include:
Use the same one which you have created.

You can check whether you are getting the value in console of the browser which can be accessed by pressing Ctrl+shift+i and clicking console tab, search for the model name to see if it printed it or not.

Please mark my answer "Helpful" and "correct" if you feel that it has helped you in any way.

Thanks and Regards,
K. Sai Charan
Sr. ServiceNow Developer
Deloitte India

hi @Sai_Charan_K 

 

I have updated as per your code, still it is not working.

 

I want to populate based on the model category, which they have selected in the serial number variable. 

 

EX:

In serial number I have selected "P1000479 - Apple MacBook Pro 15" for this one model category is "Computer". 

In Hardware item variable it needs to show only model category "Computer" list only.