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

Help with getting value of variable in multi row variable set instead of sys ID

Bruler1230
Tera Expert

Hello,

I am trying to get the value of a variable that is part of a multi row variable set. I currently am getting the sys id of the variable and cant figure out how to get the actual value. The field in question is the u_user_name field, and it currently only shows the sys id of the variable. here is my script:

var mrvs = JSON.parse(current.variables.user_info);

var user_accounts = '';

var count = 0;

for(var i=0; i < mrvs.length; i++); {

if(mrvs[i].u_user_name !=""){
var user = '';
var gr = new GlideRecord('sys_user');
gr.get(mrvs[i].u_user_name);
user += "User Name: " + mrvs[i].u_user_name;
 
}

count++;
var userCount = "User: " + count + '\n';

user_accounts += userCount + '\n' + user + '\n' + "Email: " + mrvs[i].u_email + '\n' + "Phone Number: " + mrvs[i].u_phone_number + '\n' + "Department: " + mrvs[i].u_department + '\n';

}
current.u_user_accounts += user_accounts;
1 ACCEPTED SOLUTION

for option 1;

  • Add a string variable to the variable set. 
  • Add a client script to set the new variable to the user's username.
  • Something like this in the variable set;
  • function onChange(control, oldValue, newValue, isLoading) {
        try {
            g_form.getReference('user', function(userGR){
                g_form.setValue('user_name', userGR.user_name);
            });
        } catch(error) {
            console.log(error);
        }
    }

     

for option 2;

  • Update your script
  • var mrvs = JSON.parse(current.variables.user_info);//array of objects with values
    var returnStr= '';//return string
    for(var i=0; i < mrvs.length; i++); {
      if(mrvs[i].u_user_name !=""){
        var gr = new GlideRecord('sys_user');
        if(gr.get(mrvs[i].u_user_name)){//I assume u_user_name is a ref. to sys_user
          returnStr += "User: " + '\n' + gr.getValue('user_name') + '\n';
          returnStr += "Email: " + mrvs[i].u_email + '\n' 
          returnStr += "Phone Number: " + mrvs[i].u_phone_number + '\n' 
          returnStr += "Department: " + mrvs[i].u_department + '\n';
        }
      }
    }
    current.setValue('u_user_accounts', returnStr);

 

View solution in original post

8 REPLIES 8

Jace Benson
Mega Sage

It's only storing the sys_id.  A few options;

1.  Have the variables include the user_name (which you can set via client script or what have you) that way you don't have to do another lookup

2.  Do a lookup on each user e.g. GlideRecord to sys_user

Thanks for the response..would you have an example or be able to elaborate on one or both of those options. I am kinda drawing a blank on how to do that.

Thanks.

for option 1;

  • Add a string variable to the variable set. 
  • Add a client script to set the new variable to the user's username.
  • Something like this in the variable set;
  • function onChange(control, oldValue, newValue, isLoading) {
        try {
            g_form.getReference('user', function(userGR){
                g_form.setValue('user_name', userGR.user_name);
            });
        } catch(error) {
            console.log(error);
        }
    }

     

for option 2;

  • Update your script
  • var mrvs = JSON.parse(current.variables.user_info);//array of objects with values
    var returnStr= '';//return string
    for(var i=0; i < mrvs.length; i++); {
      if(mrvs[i].u_user_name !=""){
        var gr = new GlideRecord('sys_user');
        if(gr.get(mrvs[i].u_user_name)){//I assume u_user_name is a ref. to sys_user
          returnStr += "User: " + '\n' + gr.getValue('user_name') + '\n';
          returnStr += "Email: " + mrvs[i].u_email + '\n' 
          returnStr += "Phone Number: " + mrvs[i].u_phone_number + '\n' 
          returnStr += "Department: " + mrvs[i].u_department + '\n';
        }
      }
    }
    current.setValue('u_user_accounts', returnStr);

 

Thanks...so for option 1, is userGR what you called the string variable?