onload client script not working

Priyanka132
Giga Contributor

Hi All,

I need to get the display name of all group's of which the current logged in user is a member. Although , i am able to print the group's sys_id, but the groups display name isnt coming. Any help, please

 

function onLoad() {
//Type appropriate comment here, and begin script below

var currentuser=g_user.userID;

var obj= new GlideRecord('sys_user_grmember');
obj.addQuery('user',currentuser);
obj.query();
while(obj.next())
{
var grpname=obj.group.getDisplayValue();   // display name is not coming here
alert(' logged in user is member of  : '+grpname);      

}

}

 

 

 

Thanks in advance

1 ACCEPTED SOLUTION

hvrdhn88
Giga Patron

This is the not a correct way to get the group details.. you should avoid glide record in client script. ("Best Practice")

i would suggest, mention glide record part in script include and call your function in your client script by using Glide Ajax.

 

here you go..

 

Script include:

 

var group = Class.create();
group.prototype = Object.extendsObject(AbstractAjaxProcessor, {

hello: function()
{
groupArr=[];
var json = new JSON();
var grmember = new GlideRecord('sys_user_grmember');
grmember.addQuery('user',gs.getUserID());
grmember.query();
while(grmember.next()){
groupArr.push(grmember.group.getDisplayValue());

}
var data = json.encode(groupArr);
return data;

},

type: 'group'
});

 

 

Client Script:

 

function onLoad() {
//Type appropriate comment here, and begin script below

var ga = new GlideAjax('group');
ga.addParam('sysparm_name', 'hello');
ga.getXML(HelloWorldParse);

function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var s = answer.evalJSON(); //Transform the JSON string to an object
for( var i=0 ; i < s.length ; i++) {
alert(s[i]);
}

}

}

 

 

 

 

View solution in original post

5 REPLIES 5

Tim Deniston
Mega Sage

Disclaimer: Client-side GlideRecords are not best practice. Since you need this data for the current user, you could/should use an onDisplay business rule with Scratchpad to query the data and then use an onLoad client script to work with the data. 

 

If you must use client-side GlideRecord, you will have to do an extra GlideRecord query for each group returned (or combine the sys_ids into a single GlideRecord query... the onDisplay BR is really the best way to do this). 

 

The issue is that the client-side GlideRecord only returns the sys_id of each group, not the display value. So, you would need to query for the display value of each group by using the sys_id that is returned in obj.group. 

SanjivMeher
Mega Patron

Try this

 

function onLoad() {
//Type appropriate comment here, and begin script below

var obj= new GlideRecord('sys_user_grmember');
obj.addQuery('user',g_user.userID);
obj.query();
while(obj.next())
{
var grpname=obj.group.name;   // display name is not coming here
alert('logged in user is member of  : '+grpname);      
}

}

Jag5
Tera Guru

You can use the below script to display the group name.

function onLoad() {
//Type appropriate comment here, and begin script below

var currentuser=g_user.userID;
var arr=[];

var obj= new GlideRecord('sys_user_grmember');
obj.addQuery('user',currentuser);
obj.query();
while(obj.next())
{

var gr = new GlideRecord('sys_user_group');
gr.addQuery('sys_id', obj.group);
gr.query();
while(gr.next()){
for(var i=0;i<1;i++){
arr.push(gr.name);

}

}

}
alert(' logged in user is member of : '+arr);
}

Thanks,

Jag.

 

Please mark CORRECT/HELPFUL as needed. 

hvrdhn88
Giga Patron

This is the not a correct way to get the group details.. you should avoid glide record in client script. ("Best Practice")

i would suggest, mention glide record part in script include and call your function in your client script by using Glide Ajax.

 

here you go..

 

Script include:

 

var group = Class.create();
group.prototype = Object.extendsObject(AbstractAjaxProcessor, {

hello: function()
{
groupArr=[];
var json = new JSON();
var grmember = new GlideRecord('sys_user_grmember');
grmember.addQuery('user',gs.getUserID());
grmember.query();
while(grmember.next()){
groupArr.push(grmember.group.getDisplayValue());

}
var data = json.encode(groupArr);
return data;

},

type: 'group'
});

 

 

Client Script:

 

function onLoad() {
//Type appropriate comment here, and begin script below

var ga = new GlideAjax('group');
ga.addParam('sysparm_name', 'hello');
ga.getXML(HelloWorldParse);

function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var s = answer.evalJSON(); //Transform the JSON string to an object
for( var i=0 ; i < s.length ; i++) {
alert(s[i]);
}

}

}