The CreatorCon Call for Content is officially open! Get started here.

Upender Kumar
Mega Sage

Let's cover some common scenario for MRVS with reference and lookup selectbox variables.

MRVS value is stored as a JSON format. You can get the data from existing record and pass the value to the MRVS on the form. To get the data from server, create a client callable script include and define a function to get the data. Call the method form the client side using GlideAjax depending on your requirement onChange or onLoad script. Below is the sample running code example. I have 4 variables in the variable set. User is reference field and Group is lookup.

find_real_file.png

find_real_file.png

1. Fill MRVS from same set of variables from a record

Server Side Code

getMRVS:function(){
	//var aa={};
	var gr=new GlideRecord('sc_req_item');
	gr.get('444db8712f96b01042162b5df699b654');
	var mrv=gr.variables.my_values.toString();
	return  JSON.stringify(mrv);
}

Client Side Code - onLoad client script

var gr=new GlideAjax('AjaxUtil');
gr.addParam('sysparm_name','getMRVS');
gr.getXML(GetMRVS);
function GetMRVS(response){
	var answer = response.responseXML.documentElement.getAttribute("answer"); 
	var mrv=JSON.parse(answer);
	console.log(mrv);
	g_form.setValue('my_values',mrv);
}

 Output 

find_real_file.png

In console you can see list of key-value pair objects.

find_real_file.png

2. Get data from different tables, create JSON and fill MRVS

 Server Side Code

var mrv=[];
var users=new GlideRecord('sys_user');	users.addEncodedQuery('sys_idINa94e5830dbbbb7005027fe1b68961964,8368dc69c0892300964f80d1691e1a76');
users.query();
/*
We know that we have a MRVS with first_name,last_name,user_for and group variable. So we will create a list of objects with the same keys as variables		
*/
while(users.next()){
	var mrvObj={};
	mrvObj.first_name=users.first_name.toString();
	mrvObj.last_name=users.last_name.toString();
	mrvObj.user_for=users.getUniqueValue();
	if(users.getUniqueValue()=='a94e5830dbbbb7005027fe1b68961964')
		mrvObj.group='09745cc9c3302200e7c7d44d81d3ae6f';
	else
		mrvObj.group='8ba321c22f5e281042162b5df699b639';
	mrv.push(mrvObj);
}
return  JSON.stringify(mrv);

Client Side Code

var url='';
	var gr=new GlideAjax('AjaxUtil');
	gr.addParam('sysparm_name','getMRVS');
	gr.getXML(GetMRVS);
	function GetMRVS(response){
		var answer = response.responseXML.documentElement.getAttribute("answer"); 
		g_form.setValue('my_values',answer);
	}

Output

find_real_file.png

Notes: Below are my observations

1. When populating MRVS from existing variable set data, use JSON.parse before assigning the data to the variable in client script.

2. When populating MRVS with our own JSON data, don't use JSON.parse before assigning the data to the variable in client script.

 

If the above scripts are helpful, Please mark helpful and bookmark.

Version history
Last update:
‎08-31-2021 08:28 AM
Updated by: