getReference and MRVs client script

booher04
Tera Guru

With a Client Script - I need to populate the email address of the user selected in a reference variable called ex_it_director(sys_user table) in a variable called (ex_edge_owner_email) that is within a MRVs.  Is there an easy way to do this?  I have tried to store the variable value several different ways/times with the following and variations on the catalog item(outside the MRVs):

if (typeof(Storage) !== "undefined") {
sessionStorage.setItem("ex_it_director", ""+g_form.getValue('ex_it_director'));

 

I've then tried to create an onLoad Client Script to get that value and populate the Email address(done this several different ways with no success as well):

var itDir = sessionStorage.getItem('ex_it_director');
g_form.setValue('ex_edge_owner_email', itDir.email);
 
Any help would be greatly appreciated.  I tried getReference('ex_it_director').email as well.
1 ACCEPTED SOLUTION

The script I provided would run onChange of the reference variable and populate the email address into the variable within the MRVS on every existing row.

 

If you are populating the reference field first and want every row of the MRVS that you add to automatically populate one variable with the email address of the reference field that is outside of the MRVS, all you need is an onLoad script within the MRVS like this:

 

function onLoad() {
	var userref = parent.g_form.getReference('ex_it_director', userLookup);
	function userLookup(userref){
		g_form.setValue('ex_edge_owner_email', userref.email.toString());
	}	
}

If you are using Service Portal or Employee Center there's one other step to access the parent g_form, then the script can be written to try both methods, so let me know if that applies to your scenario.

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

First use getReference to get the email address value - I guess in your scenario you would do this onChange of the reference variable, if the MRVS is already populated:

var userref = g_form.getReference('ex_it_director', userLookup);
function userLookup(userref){
    var emailAdd = userref.email.toString();
}

The value of the MRVS is stored in JSON.  You can get it from the parent form (not within the MRVS) using 

var mrvs = g_form.getValue('mrvs_internal_name');

To populate this value into the variable within the MRVS on every row, you could parse the JSON, then rebuild it using the existing values for the other variables, substituting emailAdd for the empty variable, but you can also do this just by manipulating the JSON string, so in this same script, 

mrvs = mrvs.toString().replace(/"ex_edge_owner_email":""/g, '"ex_edge_owner_email":"' + emailAdd + '"');
g_form.setValue('mrvs_internal_name', mrvs);

Notice that in the second parameter of the replace function each string value is enclosed by single quotes/apostrophes because the string contains double quotes around the name and value. So then, the entire onChange script looks like this:

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

	var userref = g_form.getReference('ex_it_director', userLookup);
	function userLookup(userref){
		var emailAdd = userref.email.toString();
		var mrvs = g_form.getValue('mrvs_internal_name');
		mrvs = mrvs.toString().replace(/"ex_edge_owner_email":""/g, '"ex_edge_owner_email":"' + emailAdd + '"');
		g_form.setValue('mrvs_internal_name', mrvs);
	}
}

 

Brad - Thanks for the response!

 

Ok, so to clarify as I may not of explained it properly.  The ex_it_director variable is a reference variable that is outside of the MRVS.  The ex_edge_owner_email address is a string variable within the MRVS.  

 

Currently I have an onChange client script outside the MRVS that gets the ex_it_director value.  I then had an onLoad Client Script that ran when I opened the MRVS(clicked ADD on the item from the portal).  Are you saying if I add this onChange outside of the MRVS and update the mrvs_internal_name(velocloud_details) that I can run that from the item outside of the MRVS?

The script I provided would run onChange of the reference variable and populate the email address into the variable within the MRVS on every existing row.

 

If you are populating the reference field first and want every row of the MRVS that you add to automatically populate one variable with the email address of the reference field that is outside of the MRVS, all you need is an onLoad script within the MRVS like this:

 

function onLoad() {
	var userref = parent.g_form.getReference('ex_it_director', userLookup);
	function userLookup(userref){
		g_form.setValue('ex_edge_owner_email', userref.email.toString());
	}	
}

If you are using Service Portal or Employee Center there's one other step to access the parent g_form, then the script can be written to try both methods, so let me know if that applies to your scenario.

Barath P
Tera Guru

Hi @Brad Bowman ,

I hope you're doing well.

I'm working similar on a ServiceNow catalog item MRVS and need some assistance. I have a reference variable named "User Name" outside of the MRVS. Inside the MRVS, there is a string field called "User ID."

I would like to populate the "User ID" field in the MRVS with the value from the "User Name" reference variable when a new row is added. Specifically, whenever a user enters a value in the "User Name" field and then clicks the "Add" button in the MRVS, the "User ID" field should automatically populate with the same value.

 

 

Could you guide me on how to achieve this functionality?

Thank you!

Best regards, 
@Barath P