Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

How Populate field in Multi Row Variable Sets base onChange Script

Dario_C
Mega Sage

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

 

10 REPLIES 10

Ankur Bawiskar
Tera Patron

@Dario_C 

what debugging have you done so far?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

jcmings
ServiceNow Employee

Can you do this in your variable set's client script? I don't know if you can dotwalk into a variable set's field (AKA variable) to addOption. I've never seen that done before.

Tai Vu
Kilo Patron

Hi @Dario_C 

Can you help clarify a bit? Do you want to:

1. Update the product_name values that users have already entered in the MRVS?

2. Or dynamically display the choice options for the product_name field based on the selezionare_il_servizio (a field outside the MRVS) while users interact with the form?

 

Thanks,

Tai Vu

Hi @Tai Vu ,

Thank you for your answer. 

I want to do the 2nd option, i want dynamically display choice options for the product_name field based on the selezionare_il_servizio (variables outside the mrvs). 

 

 

@Ankur Bawiskar 

Yes, the script include works fine and the data reaches the client script, the problem is that I don't know how to display the data in the choice option in the product_name field.