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

Reference variables and fields show a Display column for the referenced table, but the value stored is the sys_id of the record.  To lookup the user_name field from the user record you'll need a GlideAjax call to a Script Include, or getReference like in the example I provided - here it is adjusted for your scenario:

function onLoad() {

    var getUserName = g_service_catalog.parent.getReference('user_name', userLookup);
    function userLookup(getUserName){
	g_form.setValue('user_id', getUserName.user_name);
    }
}

The last mention of user_name refers to the field on the user record with that name, not the variable name as is the first mention.  I haven't tried getReference with g_service_catalog, so if it doesn't work, alert on getUserName to confirm it's not returned, then revert to parent.g_form.getReference if you're not using Service Portal.