Auto populate the data reference data

vinod6
Tera Contributor

I want to populate the Model field automatically when the Mfg Part# field is selected. Both fields are reference fields, and need to fetch the Model value based on the selected Mfg Part# record.

 

See the below scripts:

 

 

var GetModelDetails = Class.create();
GetModelDetails.prototype = {
    initialize: function() {},
    getModelName: function() {
        var mfgPartSysId = gs.getParameter('sysparm_u_mfg_part');
        var modelName = '';
        if (mfgPartSysId) {
            var modelGR = new GlideRecord('cmdb_model'); // Assuming cmdb_model is the table for Mfg Part# and Model
            if (modelGR.get(mfgPartSysId)) {
                modelName = modelGR.getValue('product_catalog_item'); // Assuming 'product_catalog_item' is the Model field in cmdb_model
            }
        }
        return modelName;
    },
   
    type: 'GetModelDetails'
};
 
On change client script:
Field name: Mfg Part#
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
var ga = new GlideAjax('GetModelDetails'); // Call the Script Include
    ga.addParam('sysparm_name', 'getModelName'); // Name of the method in Script Include
    ga.addParam('sysparm_u_mfg_part', newValue); // Pass the sys_id of the selected Mfg Part#

    ga.getXMLAnswer(function(response) {
        var modelName = response.responseXML.documentElement.getAttribute("answer"); // Get the Model name from the response
        if (modelName) {
            g_form.setValue('u_model', modelName); // Set the Model field with the returned value
        } else {
            g_form.clearValue('u_model'); // Clear the Model field if no value is returned
        }
});
Please check  script
It is help full for me 
Thanks



}
3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Your Script Include is not Client callable.  You can check the box, but it probably won't make the necessary modifications to the existing script.  You also need to correctly get the parameter passed in from the Client Script:

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

    getModelName: function() {
        var mfgPartSysId = this.getParameter('sysparm_u_mfg_part');
        var modelName = '';
        if (mfgPartSysId) {
            var modelGR = new GlideRecord('cmdb_model'); // Assuming cmdb_model is the table for Mfg Part# and Model
            if (modelGR.get(mfgPartSysId)) {
                modelName = modelGR.getValue('product_catalog_item'); // Assuming 'product_catalog_item' is the Model field in cmdb_model
            }
        }
        return modelName;
    },

    type: 'GetModelDetails'
});

Ankur Bawiskar
Tera Patron
Tera Patron

@vinod6 

your script include is not client callable

Check the updated script from Brad and let us know if you are stuck

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

Juhi Poddar
Kilo Patron

Hello @vinod6 

Try the following script to meet your requirement

script include:

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

    // Function to get the model name based on the selected Mfg Part#
    getModelName: function() {
        // Retrieve the sys_id of the Mfg Part# passed from the client
        var mfgPartSysId = this.getParameter('sysparm_u_mfg_part');
        var modelName = '';

        if (mfgPartSysId) {
            // Query the cmdb_model table based on the Mfg Part# sys_id
            var modelGR = new GlideRecord('cmdb_model');
            if (modelGR.get(mfgPartSysId)) {
                // Retrieve the model name from the cmdb_model record
                modelName = modelGR.getValue('product_catalog_item'); // Adjust field name if necessary
            } else {
                gs.error('Mfg Part# not found in cmdb_model table');
            }
        } else {
            gs.error('Invalid Mfg Part# Sys ID');
        }

        return modelName; // Return the model name
    },

    // Method type declaration
    type: 'GetModelDetails'
});

onChange Client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var ga = new GlideAjax('GetModelDetails'); // Call the Script Include
    ga.addParam('sysparm_name', 'getModelName'); // Name of the method in Script Include
    ga.addParam('sysparm_u_mfg_part', newValue); // Pass the sys_id of the selected Mfg Part#

   ga.getXMLAnswer(function(response) {
    var modelName = response; // The answer is returned directly in the response
    if (modelName) {
        g_form.setValue('u_model', modelName); // Set the Model field with the returned value
    } else {
        g_form.clearValue('u_model'); // Clear the Model field if no value is returned
    }
});
}

Note: Script include should be client callable, simply updating the script and marking it client callable to existing script will not work. Create a fresh new script include and try with the above scripts.

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar