- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2025 11:57 PM - edited 02-05-2025 11:59 PM
I have these two fields in a form on Service Portal
The u_utente_da_copiare is a Reference field to the sys_user and the u_gruppi_da_copiare is a List Collector connected to the sys_user_group.
All i want is getting the groups of the user selected on u_utente_da_copiare into u_gruppi_da_copiare.
So, i created this Script Include, callable from Client and from all scopes
Code:
var CDP_UserGroup = Class.create();
CDP_UserGroup.prototype = {
initialize: function() {},
getGroups: function() {
var userSysID = this.getParameter('sysparm_userSysID');
var groups = [];
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', userSysID);
gr.query();
while (gr.next()) {
groups.push(gr.group.toString());
}
return groups.join(',');
},
type: 'CDP_UserGroup'
};
I created this ACL, in order to enable a user with itil role to call it
and finally i created this Catalog Client Script in order to call the Script include
Code:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('CDP_UserGroup');
ga.addParam('sysparm_name', 'getGroups');
ga.addParam('sysparm_userSysID', newValue);
ga.getXMLAnswer(updateGroups);
function updateGroups(answer) {
if (answer) {
g_form.setValue('u_gruppi_da_copiare', response);
} else {
g_form.setValue('u_gruppi_da_copiare', '0bce384a1b517410e8b363d3b24bcb81'); //for debug reasons
}
}
}
I only get as result the line marked as "for debug reasons".
I had put alert() to debug and i saw that the newValue (used to call the Script Include) had a valid value and running the script by hand is correctly returns groups id's separated by comma (which is just what i want).
I think i followed all the suggestions i found in the Servicenow documentation and this forum (I've read a lot of posts) but i don't understand why the aswer variable i'm getting in return is still null (i've checked with an alert(answer)).
Could you please help me?
Fotios
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2025 12:12 AM - edited 02-06-2025 12:16 AM
your script include syntax for client callable is wront
It should be this way
var CDP_UserGroup = Class.create();
CDP_UserGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getGroups: function() {
var userSysID = this.getParameter('sysparm_userSysID');
var groups = [];
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', userSysID);
gr.query();
while (gr.next()) {
groups.push(gr.group.toString());
}
if (groups.length > 0)
return groups.join(',');
else
return '';
},
type: 'CDP_UserGroup'
});
Client script will be this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '')
g_form.clearValue('u_gruppi_da_copiare');
var ga = new GlideAjax('CDP_UserGroup');
ga.addParam('sysparm_name', 'getGroups');
ga.addParam('sysparm_userSysID', newValue);
ga.getXMLAnswer(function(answer) {
if (answer) {
g_form.setValue('u_gruppi_da_copiare', answer.toString());
} else {
g_form.setValue('u_gruppi_da_copiare', '0bce384a1b517410e8b363d3b24bcb81'); //for debug reasons
}
});
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
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-06-2025 12:12 AM - edited 02-06-2025 12:16 AM
your script include syntax for client callable is wront
It should be this way
var CDP_UserGroup = Class.create();
CDP_UserGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getGroups: function() {
var userSysID = this.getParameter('sysparm_userSysID');
var groups = [];
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', userSysID);
gr.query();
while (gr.next()) {
groups.push(gr.group.toString());
}
if (groups.length > 0)
return groups.join(',');
else
return '';
},
type: 'CDP_UserGroup'
});
Client script will be this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '')
g_form.clearValue('u_gruppi_da_copiare');
var ga = new GlideAjax('CDP_UserGroup');
ga.addParam('sysparm_name', 'getGroups');
ga.addParam('sysparm_userSysID', newValue);
ga.getXMLAnswer(function(answer) {
if (answer) {
g_form.setValue('u_gruppi_da_copiare', answer.toString());
} else {
g_form.setValue('u_gruppi_da_copiare', '0bce384a1b517410e8b363d3b24bcb81'); //for debug reasons
}
});
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
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-06-2025 12:38 AM
Thank you @Ankur Bawiskar ! Your solution was well described and correct 😀