- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 05:34 AM
I have below 2 variable, based on group selection , group member should get populated [each group has only one member]
1.approval_group: referring to sys_user_group table
2.approver: referring to sys_user table
tried below script, its not populating member, please help me here
------------------------------------------------------------------------------------
script include:
var GetApproverByGroup = Class.create(); GetApproverByGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, { getApprover: function() { var groupSysId = this.getParameter('sysparm_group'); var userSysId = ''; if (groupSysId) { var gr = new GlideRecord('sys_user_grmember'); gr.addQuery('group', groupSysId); gr.query(); if (gr.next()) { userSysId = gr.user.toString(); } } return userSysId; } });
Onchange client script : on change of group variable
function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; } var ga = new GlideAjax('GetApproverByGroup'); ga.addParam('sysparm_name', 'getApprover'); ga.addParam('sysparm_group', newValue); ga.getXML(function(response) { var approver = response.responseXML.documentElement.getAttribute("answer"); if (approver) { g_form.setValue('approver', approver); } }); }
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 06:20 AM
Hello @Bindhu1
Use below 👇 script include
var GetApproverByGroup = Class.create();
GetApproverByGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// Function to get the first group member
getApprover: function() {
var groupSysId = this.getParameter('sysparm_group');
var userSysId = '';
if (groupSysId) {
// GlideRecord to get group members
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', groupSysId);
gr.query();
// Fetch the first user in the group
if (gr.next()) {
userSysId = gr.user.toString(); // Getting user Sys ID
}
}
return userSysId; // Return user Sys ID
}
});
Use below 👇 client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('GetApproverByGroup');
ga.addParam('sysparm_name', 'getApprover');
ga.addParam('sysparm_group', newValue); // Send group Sys ID
ga.getXML(function(response) {
var approver = response.responseXML.documentElement.getAttribute("answer");
if (approver) {
g_form.setValue('approver', approver); // Set the approver field with the user Sys ID
}
});
}
Is the approver field also reference fields ? Because you are passing sys_id ?
Also, is the Client script set to UI type ALl and the script include with "Client callable" box checked ?
If all above is correct then make use of response directly to populate.
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 06:46 AM
@Bindhu1 I already recommended this method in my above response. That you can use response string directly.
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 07:22 AM
Did the script I shared work fine?
As per new community feature you can mark multiple responses as correct.
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
04-02-2025 01:58 AM
Did the script I shared work fine?
If yes then you can mark my response correct as well and it's allowed as per new community feature.
Thanks in advance !
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 06:19 AM
Hi @Bindhu1
Try below script. I just tested out in my PDI.
Script include:
getApprover: function() {
var groupSysId = this.getParameter('sysparm_group');
var userSysId = '';
if (groupSysId) {
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', groupSysId);
gr.query();
if (gr.next()) {
userSysId = gr.user.toString();
}
}
return userSysId;
},
Client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('GetApproverByGroup');
ga.addParam('sysparm_name', 'getApprover');
ga.addParam('sysparm_group', newValue);
ga.getXMLAnswer(getResponse);
//Type appropriate comment here, and begin script below
}
function getResponse(answer) {
if (answer) {
g_form.setValue('user', answer);
}
}
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2025 06:20 AM
Hello @Bindhu1
Use below 👇 script include
var GetApproverByGroup = Class.create();
GetApproverByGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
// Function to get the first group member
getApprover: function() {
var groupSysId = this.getParameter('sysparm_group');
var userSysId = '';
if (groupSysId) {
// GlideRecord to get group members
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', groupSysId);
gr.query();
// Fetch the first user in the group
if (gr.next()) {
userSysId = gr.user.toString(); // Getting user Sys ID
}
}
return userSysId; // Return user Sys ID
}
});
Use below 👇 client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('GetApproverByGroup');
ga.addParam('sysparm_name', 'getApprover');
ga.addParam('sysparm_group', newValue); // Send group Sys ID
ga.getXML(function(response) {
var approver = response.responseXML.documentElement.getAttribute("answer");
if (approver) {
g_form.setValue('approver', approver); // Set the approver field with the user Sys ID
}
});
}
Is the approver field also reference fields ? Because you are passing sys_id ?
Also, is the Client script set to UI type ALl and the script include with "Client callable" box checked ?
If all above is correct then make use of response directly to populate.
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY