Creating a service request for raising access request.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2019 02:31 AM
Hello everyone,
I am creating a service request to raise access request. Here i have one field which will show the group names to which user is already member or not a member depending on the option he select for request type(grant access or remove access).
If user selects Grant access then there all groups should be available to user to choose from except those to which he is already a member.
Similarly when user selects remove access option than only those groups should be visible to which he is a member.
I have created a script include to fetch these details in two arrays var existingGrp=[]; it will store those groups to which user is a member and var groupIDs = []; will store rest of the group names to which user is not a member.
Below is my code in script include:-
var getSnowGroups = Class.create();
getSnowGroups.prototype = {
getAdditionGroups:function() {
var usrid=current.variables.v_requested_for;
if (usrid=='')
return '';
var groupIDs = [];
var existingGrp=[];
var gr= new GlideRecord("sys_user_grmember");
gr.addQuery('user',usrid);
gr.query();
while(gr.next())
{
existingGrp.push(gr.getValue('group'));
}
var grp=new GlideRecord("sys_user_group");
grp.addActiveQuery();
grp.addQuery('sys_id','NOT IN',existingGrp);
grp.query();
while(grp.next())
{
groupIDs.push(grp.name.toString());
}
gs.log("full groups "+groupIDs.length);
return 'sys_idIN'+groupIDs; // + groupIDs.join(',');
},
type: 'getSnowGroups'
};
Can anyone please help me here why two arrays are not having any data.
Regards,
Nisha
- Labels:
-
Service Catalog

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2019 02:47 AM
Hi there,
The arrays are empty your mentioning. Have you already debugged until where the script include is working?
Could you share how you are calling this Script Include?
Apart from this, I'm wondering if the start of the Script Include is already the issue. You are mentioning:
var usrid=current.variables.v_requested_for;
Though, is this working? If you write usrid for example to the log, do you get the correct value? I would expect you should pass "current" somewhere into your script include, or because this should happen client side and onchange, that you are working with GlideAjax and retrieving the user id with for example something like:
this.getParameter('sysparm_field_passed_from_client_script');
Kind regards,
Mark
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2019 02:52 AM
Hi there,
Are you calling this script include thru for example the reference qualifier of a reference variable? Can you show the code for that?
Also how far did you get debugging this? Is there a specific where it does work/doesn't work, are the arrays just blanc, etc.. Is this line working for example?
var usrid=current.variables.v_requested_for;
Kind regards,
Mark
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2019 02:54 AM
Maybe this already helps as inspiration. I've got a similar situation with retrieving a list of sys_ids based on in this case the user_id in a other reference variable.
function getAdminAccessCI() {
// Define array
var listGroups = [];
// Get records
var getGroups = new GlideRecord('qt_admin_access_admin_account');
getGroups.addEncodedQuery("user_name=javascript:gs.getUserID()^active=true");
getGroups._query();
while(getGroups._next()) {
// Fill array
listGroups.push(getGroups.ci.toString());
}
// Return encoded query
return "sys_idIN" + listGroups + "^active=true";
}
Kind regards,
Mark
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2019 03:46 AM
Dear Nisha,
Assuming that you are working in Service Catalog and have at least 3 variables:
1) Requested For (requested_for) - referencing to the user table
2) Request Type (request_type) - 2 options (questions) -> 'grant_access', 'remove_access'
3) Groups - List Collector type reference to the group table with Reference Qualifier as:
javascript:new getSnowGroups().getGroups(current.variables.request_type, current.variables.requested_for)
A script include like shall work
var getSnowGroups = Class.create();
getSnowGroups.prototype = {
getGroups:function(requestType, requestedFor) {
var ret = 'sys_idIN';
gs.log('---> requestType: '+requestType+', requestedFor: '+requestedFor);
if (requestedFor == '' || requestedFor == undefined || requestType == '' || requestedFor == undefined)
return ret;
if (requestType == 'grant_access')
return ret+this.getRemainingGroups(requestedFor);
else if (requestType == 'remove_access')
return ret+this.getUsersGroups(requestedFor);
return ret;
},
getUsersGroups: function (userID) {
var existingGrp=[];
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery('user', userID);
gr.query();
if (gr.getRowCount() == 0)
return '';
while(gr.next())
existingGrp.push(gr.getValue('group'));
gs.log('---> Existing Groups: '+existingGrp.toString());
return existingGrp.toString();
},
getRemainingGroups: function (userID) {
var groupIDs = [];
var existingGrp = this.getUsersGroups(userID);
var grp=new GlideRecord("sys_user_group");
grp.addActiveQuery();
grp.addQuery('sys_id','NOT IN', existingGrp);
grp.query();
if (grp.getRowCount() == 0)
return '';
while(grp.next())
groupIDs.push(grp.getValue('sys_id'));
gs.log('---> Remaining Groups: '+groupIDs.toString());
return groupIDs.toString();
},
type: 'getSnowGroups'
};