Need to populate the group names which opened by user belongs to.

arunkumar_dharm
Kilo Expert

Hi Guys,

I need to populate the group names which "opened by" user belongs to on incident form. I've tried the below script but it gives me only the sys_id value, but i need the display value. Also i would like to show an alert message on the top of the form. I know it's not a good practice of using glide record in client side but help me to achieve this in any of the way.

function onLoad() {

var group_ids = [];
var user_id = g_form.getValue('opened_by')+'';
var grm_rec = new GlideRecord('sys_user_grmember');
grm_rec.addQuery('user',user_id);
grm_rec.query();
while(grm_rec.next()){

group_ids.push(grm_rec.group);

}
if(group_ids.length>0){
g_form.setValue('close_notes',group_ids+'');

}}

 

Regards,

Arun

1 ACCEPTED SOLUTION

Hi,

using gliderecord is not a best practice,you have to use client script + glide ajax to retrieve all groups on client side.

I can help you on the logic on server side which you can get on the client side,

script include:

Name:getCallerGroups

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

getGroups: function()
{
var sysid = this.getParameter('sysparm_user_name');

var grp =[];

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

grp.push(rec.group.getDisplayValue().toString());
}
gs.log("munender"+grp);
return grp.toString();
},




type: 'getCallerGroups'
});

 

Client script:

function onLoad() {

var caller = g_form.getValue('caller_id');

var ga = new GlideAjax('getCallerGroups');
ga.addParam('sysparm_name', 'getGroups');
ga.addParam('sysparm_user_name', caller);

ga.getXML(HelloWorldParse);

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

}

 

Regards,

Munender

 

View solution in original post

14 REPLIES 14

Hi Munender,

Thanks a ton. It's working fine but could please help me on how we can populate this value into a string field.

Also kindly help me on how we can get those groups one by one in the alert message.

 

Regards,

Arun

Hi,

Please specify how you want to set into the string field

second,to get them one by one

onclient side:
var ans = answer.split(',');
var ans1 = ans[0];
var ans2 = ans[1];
var ans3 = ans[2];
alert(ans1);
alert(ans2);
alert(ans3);

Regards,
Munender

Hi Munender,

Thanks for the reply again.

I want to set this value into "work notes" field one by one.

 

Thanks,

Arun

Hi Munender,

Regarding one by one alert,

If an user is part of 5+groups it will display only 3 [As per the above code]. Is there any way to display all the user group even if he is part of 50 groups one by one in a single alert. Customer don't want multiple alerts. Kindly help me on this.

 

Regards,

Arun

Hi,

Any update on this?
Can you mark my answer as correct, helpful if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Thanks in advance.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader