- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-17-2017 01:26 AM
Have a requirement as below:-
Created a watchlist field(u_groups) to select many groups.
Condition is, if in selected group atleast one member has "approver" role then only the group should be selected else it should display the field name and message.
Any code will be helpful.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-17-2017 08:58 AM
Hi Iqubal
1. you were looking to display some message when you are selecting the group name. For this you need to create a client script onChange.
2. What is current_u_groups ?? If you are looking for the current group value then it should be current.u_groups. but current does not work in the client script. you need to use g_form
3. You need to create a glideajax script. the client script will call that glide ajax. and the script which you have written will go in glide list. this glide list should return Yes or no or some other value.
4. then in your cliet script you can show this value in message
To understand about the glideajax please see the link - http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2017 04:15 AM
Hi Sharique,
"u_groups" is my watchlist field.
I created script include "getUserRoles". It runs fine in background script and I can get 2 result as :-
*** Script: BG L3 Sine,RMA Approvers
I put this into reference qualifier of field "u_groups". //javascript: new getUserRoles().getUserRoles();
Now, when I select group in "u_groups" I get a list of all groups. I want when I select any group the script should run and able to select only those group which qualifies the script include.
But, when I select group it display the message correctly but able to select any group.
SCript Include:-
var getUserRoles = Class.create();
getUserRoles.prototype = {
initialize: function() {
},
getUserRoles:function() {
var answer = [];
var groupMember = new GlideRecord('sys_user_grmember');
groupMember.addQuery('group', current.getValue('u_groups'));
gs.addInfoMessage(current.u_groups);
groupMember.query();
while(groupMember.next()){
var user_sys_id = groupMember.getValue('user');
var hr = new GlideRecord('sys_user_has_role');
hr.addQuery('user', user_sys_id);
hr.addQuery('role', 'e098ecf6c0a80165002aaec84d906014'); //sys_id of role
hr.query();
if(hr.next())
{
answer.push(groupMember.getDisplayValue('group'));
}
else{
answer.push("You cannot select this group as none of users are having approver role");}
}
gs.addInfoMessage(answer);
return "group.sys_idIN"+ answer;
},
type: 'getUserRoles'
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2017 04:41 AM
Hi,
<Update> I had to write this after i wrote the code: How can you pass the values from the same field on which you want to set the reference qualifier??
If u_groups is some another field on the form, then the below code works fine... Else you should some another logic.. because in the query for groupMember.->'group', grp you are actually passing the values from the field which is about to be set. so now you are actually passing no values to the script include.. refer Reference qualifier on catalog item using script include to exclude records dynamically
Like i said earlier, Can you please change the function to function getUserRoles(grp) {... }
Reference Qualifier: javascript: new getUserRoles().getUserRoles(current.u_groups);
In the code
var answer = [];
var groupMember = new GlideRecord('sys_user_grmember');
groupMember.addQuery('group', grp); //changed it to grp
//gs.addInfoMessage(current.u_groups); //wont work
groupMember.query();
while(groupMember.next()){
var user_sys_id = groupMember.getValue('user');
var hr = new GlideRecord('sys_user_has_role');
hr.addQuery('user', user_sys_id.toString());
hr.addQuery('role', 'e098ecf6c0a80165002aaec84d906014'); //sys_id of role
hr.query();
if(hr.next())
{
answer.push(groupMember.getDisplayValue('group')); // by this you are passing the sys_id of the groups, use 'group.name' if you want the names to be returned
}
else{
//answer.push("You cannot select this group as none of users are having approver role");
You cannot push some thing like this to return back. use return false; instead
}
}
gs.log("This is test "+answer);
gs.addInfoMessage(answer);
return "group.sys_idIN"+ answer.join(",");
Else,this has to be the code,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-17-2017 02:01 AM
HI iqbul, Could you please provide mor e inforamtion on your requirment please find the below link it may help you https://docs.servicenow.com/bundle/jakarta-servicenow-platform/page/script/server-scripting/concept/... https://community.servicenow.com/thread/173086