We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Populate User Details in Multi Row Variable Set on change of Group

vijay39
Giga Expert

Hello Guys,

I have created a catalog item with a Group field (reference) and there is a multi row variable set with 3 variables in it. My requirement is, when i select any group in the Group field, then the members of that selected group must auto populate in that Multi row variable set. HOw can i do this? 

i have seen this link but the code is little confused to my understanding. Can some one help me here?

https://community.servicenow.com/community?id=community_article&sys_id=86971c8bdb4108106064eeb5ca961...

 

Regards,

Vijay

13 REPLIES 13

Kunal Varkhede
Tera Guru

Hi,

 

Try below code 

In Script include

var mvrs = Class.create();
mvrs.prototype = Object.extendsObject(AbstractAjaxProcessor, {
groupMembers:function()
{

var something = this.getParameter('sysparm_details');
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group',something);
gr.query();


var result =
{
name:" ",
email:" ",
location:" "
};

while(gr.next())
{
var user = gr.user;

var user_rec = new GlideRecord('sys_user');
user_rec.addQuery('sys_id',user);
user_rec.query();
while(user_rec.next())
{
gs.log("inside second while"); ------------------ this is coming in log

result.name:user_rec.name;

result.email:user_rec.email;

result.location:user_rec.location.getDisplayValue();

};  //user while close

return JSON.stringify(result);

} //grmember while is closed

},//groupMembers function is close
type: 'mvrs'
});



Client script:

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

//Type appropriate comment here, and begin script below
var ga = new GlideAjax('mvrs');
ga.addParam('sysparm_name', 'groupMembers');
ga.addParam('sysparm_details',newValue);
ga.getXML(HelloWorldParse);

function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);

var res = JSON.parse(answer);
g_form.setValue('field_name',res.name);
g_form.setValue('field_name',res.email);
g_form.setValue('field_name',res.location);

}

}

I hope it will help you.

 

Please Mark Correct/Helpful answer if it help you in any way.

Thanks,

Kunal.

Hi Kunal,

 

in these 3 lines, 

result.name:user_rec.name;

result.email:user_rec.email;

result.location:user_rec.location.getDisplayValue();

it is showing errors. 

i have modified my SI and on CHange CS

SI:

var mvrs = Class.create();
mvrs.prototype = Object.extendsObject(AbstractAjaxProcessor, {
groupMembers:function()
{
var ar = [];
var arr = [];
var something = this.getParameter('sysparm_details');
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group',something);
gr.query();

while(gr.next())
{
var user = gr.user;
gs.log("user is "+user);
ar.push(user.toString());
var user_rec = new GlideRecord('sys_user');
user_rec.addQuery('sys_id',user);
user_rec.query();
while(user_rec.next()){
gs.log("inside second while");
var name = user_rec.name;
var email = user_rec.email;
var location = user_rec.location;
gs.log("User details "+name+" "+email+" "+location);
arr.push(name+" "+email+" "+location);
}
return user_rec.name+","+user_rec.email+","+user_rec.location;
}


},
type: 'mvrs'
});

 

.........................................................................

CS:

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

//Type appropriate comment here, and begin script below
var arra = [];
var ga = new GlideAjax('mvrs');
ga.addParam('sysparm_name', 'groupMembers');
ga.addParam('sysparm_details',newValue);
ga.getXML(HelloWorldParse);

function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
var ans = answer.toString();
var spl = ans.split(',');
g_form.setValue('testing_populations',JSON.stringify(spl[0]));

}

}

.........................................................................................................

in the alert it is showing

find_real_file.png

and to set the value in a MVRS, g_form.setValue('testing_populations',JSON.stringify(spl[0]));

here testing_populations is my variable set name. Not understanding how to set the name, email and location coming in alert to the variables in MRVS. 

Also, there are 4 members in the selected group but why only one user is coming in my alert? Anything wrong in my SI?

 

Regards

VIjay

Try this, var spl = ans.split(','); g_form.setValue('name of variable in variable set',arr[0]);//here pass variable name not variable set g_form.setValue('name of variable in variable set',arr[1]); g_form.setValue('name of variable in variable set',arr[2]);

Hello Vijay,

Did you get any solution for this? I have similar requirement.

Thanks in advance