How to populate values in MRVS automatically from List type variable on catalog item

rkreddy
Giga Expert

Hi All,

I Have a List type variable on Catalog form, referring to user table. And I have MRVS on the same form, with the variables like name, email, department, telephone. If I select any user in List Type Variable, I need to populate the user related information in MRVS, like his name, email, location, telephone. And If I remove user from List Variable, related row needs to be deleted from MRVS. The same should work on MRVS too, like if i remove row from MRVS, the related user should be removed from List variable.

find_real_file.png

Thanks in Advance

1 ACCEPTED SOLUTION

Hi,

I assume your onChange is written on the Attendee List field

Updated below

Script Include:

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

    listcollector:function() {    

        var listValuename = [];
        var userInfo = this.getParameter('sysparm_user_info'); //Passing the selected list collector asset value.
        var query = 'sys_idIN' + userInfo;
        if(userInfo)
        {            
            var gr = new GlideRecord('sys_user');
            gr.addEncodedQuery(query);
            gr.query();
            while (gr.next()){
                //Push selected list collector values into multi row variable set variables.
                // use the correct mrvs variable name here for attendee_name,email_address,department
                listValuename.push({
                    "attendee_name": gr.getValue('sys_id'),  
                    "email_address": gr.getValue('email'),
                    "department":gr.getDisplayValue('department')
                });
            }
        }
        return JSON.stringify(listValuename);
    },
    type: 'populateEmailfromList'

});

Client Script:

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

    var listValuename = g_form.getValue('attendees'); //Getting the list collector values.

    var ga = new GlideAjax('populateEmailfromList'); //Calling Script Include.
    ga.addParam('sysparm_name', 'listcollector'); // Function name of the script include.
    ga.addParam('sysparm_user_info', listValuename); // Passing selected list collector value as parameter.
    ga.getXML(listcolleValues);

    function listcolleValues(response){

        var val = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('variables.attendees_information', val); //Set multi row variable set with the selected list collector values.
    }
}

Regards
Ankur

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

View solution in original post

23 REPLIES 23

@rkreddy 

You need to use onChange client script and get the JSON by passing the list values

refer below link for script help

change fields value

Why to have this logic -> if MRVS row is removed then remove the user from list. You cannot have onChange catalog client script on MRVS variable. So this won't work

Regards
Ankur

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

Hi Ankur, is there any way to check whether the users in List field are in MRVS or not. If user is there in List and not MRVS can we make any alert? the length doesnot work here because we are allowing users to add rows manually into MRVS, so we need another way to check whether the user in List is present in MRVS or not? if not get alert.

Hi,

yes you can parse the JSON and check but this would be easier.

Update the MRVS directly based on the list users rather than checking which is present and absent

Also if you already have some JSON data in the MRVS then you can append the users by forming the complete json again

Regards
Ankur

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

Thanks Ankur,

Update the MRVS directly based on the list users rather than checking which is present and absent:

But we are also allowing users to add user data manually, who are not in list variable. So, MRVS contains combination of rows copied from list values and manually entered values. 

I am not good with JSON, could you please provide script?

I have written below SI and onChange for copying values from List to MRVS.

Script include:

var populateEmailfromList = Class.create();
populateEmailfromList.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	listcollector:function() {	
		
		var val=0;
		var listValuename = [];
		var userInfo = this.getParameter('sysparm_user_info'); //Passing the selected list collector asset value.
		var query = 'sys_idIN'+userInfo;
		if(userInfo)
			{			
				var gr = new GlideRecord('sys_user'); 
				gr.addEncodedQuery(query);
				gr.query();
				while (gr.next()){
					//Push selected list collector values into multi row variable set variables.
					listValuename.push({
						"attendee_name": gr.getValue('sys_id'),  
						"email_address": gr.getValue('email'), 
					"department":gr.getDisplayValue('department')
					});
			}
		}
		val = JSON.stringify(listValuename);
		return val;
	},
	type: 'populateEmailfromList'
	
});

 

onChange:

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

	var listValuename = g_form.getValue('attendees'); //Getting the list collector values.
	
	var ga = new GlideAjax('populateEmailfromList'); //Calling Script Include.
	ga.addParam('sysparm_name', 'listcollector'); // Function name of the script include.
	ga.addParam('sysparm_user_info', listValuename); // Passing selected list collector value as parameter.
	ga.getXML(listcolleValues);

	function listcolleValues(response){

		var val = response.responseXML.documentElement.getAttribute("answer");
		g_form.setValue('attendees_information',val); //Set multi row variable set with the selected list collector values. 
				} 
	}

Hi,

I assume your onChange is written on the Attendee List field

Updated below

Script Include:

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

    listcollector:function() {    

        var listValuename = [];
        var userInfo = this.getParameter('sysparm_user_info'); //Passing the selected list collector asset value.
        var query = 'sys_idIN' + userInfo;
        if(userInfo)
        {            
            var gr = new GlideRecord('sys_user');
            gr.addEncodedQuery(query);
            gr.query();
            while (gr.next()){
                //Push selected list collector values into multi row variable set variables.
                // use the correct mrvs variable name here for attendee_name,email_address,department
                listValuename.push({
                    "attendee_name": gr.getValue('sys_id'),  
                    "email_address": gr.getValue('email'),
                    "department":gr.getDisplayValue('department')
                });
            }
        }
        return JSON.stringify(listValuename);
    },
    type: 'populateEmailfromList'

});

Client Script:

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

    var listValuename = g_form.getValue('attendees'); //Getting the list collector values.

    var ga = new GlideAjax('populateEmailfromList'); //Calling Script Include.
    ga.addParam('sysparm_name', 'listcollector'); // Function name of the script include.
    ga.addParam('sysparm_user_info', listValuename); // Passing selected list collector value as parameter.
    ga.getXML(listcolleValues);

    function listcolleValues(response){

        var val = response.responseXML.documentElement.getAttribute("answer");
        g_form.setValue('variables.attendees_information', val); //Set multi row variable set with the selected list collector values.
    }
}

Regards
Ankur

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