Need help for Catalog UI Policy Script using AJAX calls
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2018 03:57 AM
Hi All,
Need some advice on the following:
I have a variable named "Groups" in my Catalog Item, i am trying to populate the list of groups that a portal requestor belongs to into the variable when the requestor is submitting a new request.
(He/She can choose the group that is required)
I did some testing and managed to do this in the background script, can someone help to advice how to write something similar under Catalog UI Policy -> Script using AJAX calls?
var groups = [];
var gr = new GlideRecord('sys_user_grmember');
var userID = gs.getUserID();
gr.addQuery('user', userID);
gr.queryNoDomain();
while(gr.next())
{
groups.push(gr.group);
}
Thank you for your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2018 04:07 AM
Hi Alex,
So you want to have this in GlideAjax. here are the steps
1) create script include with client callable checkbox
Script:
var getGroups = Class.create();
getGroups.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getGroupArray: function(sysId){
var groups = [];
var gr = new GlideRecord('sys_user_grmember');
var userID = gs.getUserID();
gr.addQuery('user', userID);
gr.queryNoDomain();
while(gr.next())
{
groups.push(gr.group.toString());
}
return groups;
},
type: 'getGroups'
};
2) client script:
var ga = new GlideAjax('getGroups') ; ga.addParam('sysparm_name','getGroupArray'); ga.getXMLWait(); alert(ga.getAnswer()); // this will alert the array of groups; but remember this would be array of sys_id
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
‎08-16-2018 04:54 AM
Hi Ankur Bawiskar,
Thanks for your reply. Is there a way to input the client script into a catalog client script. I am encountering the following error currently.
When a User select a group that he/she belongs to encounter the following error.
I would like to achieve the following, drop down return a list of groups where user can choose a group in the variable
Can you advice what needs to be change to achieve this result?
Thank you in advance for your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-16-2018 05:29 AM
Hi Alex,
Your script should take user id and it is fetching the groups for that user. which variable on the catalog item is a reference to the user table
var ga = new GlideAjax('getGroups') ; ga.addParam('sysparm_name','getGroupArray');
ga.addParam('sysparm_userId', g_form.getValue('<userVariable>')); // you need to send the selected user from variable ga.getXMLWait(); alert(ga.getAnswer());
Following change in script include
var groups = [];
var gr = new GlideRecord('sys_user_grmember');
var userID = this.getParameter('sysparm_userId'); // this value is sent from client script
gr.addQuery('user', userID);
gr.queryNoDomain();
while(gr.next())
{
groups.push(gr.group.toString());
}
return 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
‎08-24-2018 07:17 AM
Hi Alex,
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader