How to auto-populate Multi-Row Variable Set?

MarioP1
Tera Contributor

Hello Community,

What is the best approach to auto-populate fields on 'Multi-Row Variable Set' based on 'OnChange' of reference variable on catalog item?

For example:  Lets say User has selected following value on reference variable on catalog item:

find_real_file.png

Now the above selected value needs to be auto-populated on multi-row variable set below:

find_real_file.png

Please advise the best way to achieve this.

Thanks

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@MarioP 

you can create onChange Client Script on that Reference variable and set the JSON string of the MRVS

I have shared solution here; adding to this post as well

How to populate Requestor Details in variables of MRVS?

Sample Script Below:

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

    listcollector:function() {    

        var listValuename = [];
        var userInfo = this.getParameter('sysparm_user_info');
        var query = 'sys_idIN' + userInfo;
        if(userInfo)
        {            
            var gr = new GlideRecord('sys_user');
            gr.addEncodedQuery(query);
            gr.query();
            if(gr.next()){
                
                listValuename.push({
                    "name": gr.getValue('name'), 
                    "title": gr.getValue('title')
                });
            }
        }
        gs.info('ARB JSON'+JSON.stringify(listValuename));
        return JSON.stringify(listValuename);
    },
    type: 'populateEmailfromList'

});

onChange Client Script:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}

	if(newValue == ''){
		g_form.clearValue('mrvsVariableName'); // give name of MRVS variable set here
	}

	if(oldValue != newValue){
		var ga = new GlideAjax('populateEmailfromList'); 
		ga.addParam('sysparm_name', 'listcollector');
		ga.addParam('sysparm_user_info', g_form.getValue('requestor')); // give here the requestor variable name
		ga.getXML(listcolleValues);

		function listcolleValues(response) {
			var val = response.responseXML.documentElement.getAttribute("answer");
			g_form.setValue('mrvsVariableName', val);  // give name of MRVS variable set here
		}
	}
}

Regards
Ankur

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

View solution in original post

14 REPLIES 14

Hi Ankur,

Thanks for sharing the solution. I tried it and seems to work with an issue.

Alert does show data, so script is fetching correct data:

However, MVRS is empty(Don't see message 'No data to display' which is good):

Can you advise?

 

Thanks

Thanks Ankur. Your solution was perfect.

This leads to another query where I need to populate another MRVS based on selection on MVRS.

 

For example:

find_real_file.png

So need to populate following MRVS based on App Code selected in above MRVS:

find_real_file.png

Have you worked on something similar?

Thanks

@MarioP 

I haven't tried onChange of MRVS.

there might be some limitations in terms of this.

Regards
Ankur

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

Where should we add this onchange client script? here?

find_real_file.png

 

onChange of the variable Name

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