- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 03:32 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 04:14 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 03:40 AM
Hi Arun,
update code to below
group_ids.push(grm_rec.group.toString());
also what type of field is close_notes? if it is a string field then it is fine
also you cannot dot walk and get grm_rec.group.name in client side; since 2 dot walk not supported in client side
I would suggest to have GlideAjax and script include and return the values from there
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 03:49 AM
Hi Ankur,
Thank you so much for the reply. I've tried the same but still working same as before.
Also how we can set this group names as info message on the form. Kindly suggest.
Regards,
Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 04:08 AM
Hi Arun,
You can only populate sys_ids in that close notes field since you cannot dot walk twice and hence cannot fetch group name. for that you need to use script include and GlideAjax. It will work when using script include and Ajax
you can show the group names as info message using g_form.addInfoMessage('Groups are: ' + groups);
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2019 04:14 AM
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