Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Harsh Vardhan
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

Upender Kumar
Mega Sage

Hi,

It is advised to use display Business Rule. We should avoid GlideRecord API in client script API.

 

Use display business rule and store data in g_scratchpad variable and access this on client side. 

 

BR code

current.g_scratchpad.variable=values;

 

 

Client Side

onLoad Script

alert(g_form.g_scratchpad.variable)

 

 

Thanks