How Populate field in Multi Row Variable Sets base onChange Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2025 12:32 PM - edited ‎02-04-2025 12:34 PM
Hi everyone,
I have an issue with my catalog client script. I need to change the value of one field within a multi-row variable set, based on the location of a field in a catalog item (i.e., outside the catalog item). I am using a Script Include to retrieve
all the data from the server side, but I can't load this data into the field. I don't know what I'm doing wrong.
Script Include
c
getProductByDistrict: function() {
var selezionare_il_servizio = this.getParameter('sysparm_dept_sys_id');
gs.log('getProductByDistrict: ' + 'QUERY SERVICE CONFIGURATION');
var grService = new GlideRecord('u_service_configuration');
if (grService.get(selezionare_il_servizio)) {
var nameLocation = grService.getValue('u_service');
gs.log('getProductByDistrict nameLocation ' + nameLocation);
}
var nameLocationUpper = nameLocation.toUpperCase();
gs.log('getProductByDistrict: ' + 'QUERY CMN LOCATION');
var grLocation = new GlideRecord('cmn_location');
grLocation.addEncodedQuery('nameSTARTSWITH' + nameLocationUpper);
grLocation.query();
if (grLocation.next()) {
var district = grLocation.sys_id;
gs.log('getProductByDistrict LOCATION ' + 'LOCATION: ' + grLocation.name + ' SYS_ID: ' + grLocation.sys_id);
}
gs.log('getProductByDistrict: ' + 'PRODUCT MODEL');
var gr = new GlideRecord('cmdb_consumable_product_model');
gr.addQuery('u_district', district);
gr.query();
gs.log('getProductByDistrict log finale query ' + district);
var count = 0;
var productNames = [];
while (gr.next()) {
productNames.push({
//sys_id: gr.getValue('sys_id'),
name: gr.getValue('name')
});
}
return JSON.stringify(productNames);
}
Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var distretto = g_form.getValue('selezionare_il_servizio');
var ajax = new GlideAjax('xxxx');
ajax.addParam('sysparm_name', 'getProductByDistrict');
ajax.addParam('sysparm_dept_sys_id', distretto);
ajax.getXMLAnswer(function(answer) {
if (answer) {
var productIds = answer.split(',');
JSON.parse(productIds);
g_form.clearOptions('product_model');
for (var i = 0; i < productIds.length; i++) {
var idItem = productIds[i].trim();
if (idItem !== "") {
g_form.addOption(medium_low_technology_produc_list.product_name, idItem, idItem);
// medium_low_technology_produc_list is a name of Variable Set
// product_name is an name of field inside the variable set where i need to put the data
}
}
}
});
}
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2025 02:10 AM
@Dario_C Yeah man, so what I suggested before should do the trick.
We need an OnLoad Client Script inside the MRVS instead of OnChang outside the catalog item. Then we use this below line to retrieve the current selected district.
var distretto = g_service_catalog.parent.getValue('selezionare_il_servizio');
And the rest of your code should be the same.
Cheers,
Tai Vu