Auto populate the data reference data
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2024 02:05 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2024 04:23 AM
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'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-17-2024 04:29 AM
your script include is not client callable
Check the updated script from Brad and let us know if you are stuck
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2024 02:20 AM
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