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-05-2025 01:46 AM
you can set the MRVS variable value but I don't think you can add the option using that logic.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2025 02:00 AM
Hi @Dario_C
Thanks for your information. Based on your explanation, you may want to use g_service_catalog.
g_service_catalog - getValue(String variableName): Returns the value of the specified field on the catalog item form when used in a client script on multi-row variable sets (MRVS).
So the approach would be:
1. Create an OnLoad Client Script in the MRSV.
2. When the user enter the MRSV and open the modal, we get the value of "selezionare_il_servizio" from the main form.
3. Call the AJAX script include like you did.
4. Add and remove the choice options of "product_name" accordingly.
So this line below should be changed from:
var distretto = g_form.getValue('selezionare_il_servizio');
to
var distretto = g_service_catalog.parent.getValue('selezionare_il_servizio');
In the other hand, you can consider to handle the choices available in the Product field with a variable type that has Reference Qualifier. This approach is more efficient, as client-side handling can impact performance, especially if there are many choices to add or remove dynamically.
Hope it works out.
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-05-2025 02:27 AM
Hi @Tai Vu ,
my mystake, i want the first one option, so update the product_name values that users have already entered in the MRVS, because my Catalog client Script is in Global Spoke inside the Catalog Item, so selezionare_il_servizio is on Item, and i want update the values of product_name when user open the variable Set. I put here the photo of item:
This is the variable set with the field i want update with addOption.
This is the field on Catalog Item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2025 08:18 PM
Hey @Dario_C
Updating (setting a value) for a variable and adding choice options for it are 2 different things.
As far as I understand your scenario:
- On the main form, there is a field "Selezionare il Distretto".
- The user selects a district.
- The user clicks on "Add Item" in the MRVS, and the modal appears.
- The user clicks on the "Product Name" field.
- The available choices for "Product Name" are displayed according to the selected District.
In the other hand, if the user has already added some items (products) to the MRVS but then decides to change the selected District (Selezionare il Distretto) to another, you could simply empty the MRVS and require them to re-add new products, ensuring that the data aligns with the newly selected district.
Let me know if I misunderstood your case! 😊
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2025 01:15 AM
Yes, exatcly !
The issue, i think is "Selezionare il Distretto" is on Catalog Item and "Product Name" is a part of MRVS. I have tried to test with a field test like " Product Name" directly on catalog Item, and it is work fine ! I think the problem is the closed structure of MRVS.