- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2016 06:41 AM
I am very new to SN and JS. I am trying to understand how to add multiple groups to the client and server scripts below. The script is used to limit who can open a priority 1 incident.
What is the syntax or proper method to add a second (or third group) group using this method (e.g. add a "mytestgroup2" group)? The easiest method I can think is to add a second business rule, but I am sure it is easier than that.
Client Script:
function onLoad() {
//Type appropriate comment here, and begin script below
//g_form.addInfoMessage('Scratchpad : ' + g_scratchpad.isMember);
//Called the g_scratchpad.isMember funtion within the Display Business Rule
if (g_scratchpad.isMember == true)
{
g_form.removeOption('impact', '1');
g_form.removeOption('urgency', '1');
}
}
Business Rule:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
g_scratchpad.isMember = gs.getUser().isMemberOf('mytestgroup1');
})(current, previous);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2016 07:09 AM
You can just add or condition in the BR
if(gs.getUser().isMemberOf('mytestgroup1') || gs.getUser().isMemberOf('mytestgroup2') || gs.getUser().isMemberOf('mytestgroup3')){
g_scratchpad.isMember=true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2016 07:06 AM
I assume that the user can only belong to one group : either group& Or group 2 or group 3.
Then within the script I provided above, add the sys_id of these groups within an Or condition
var person = g_user.userID;
var ir = new GlideRecord('sys_user_grmember');
var qc = ir.addQuery('group', 'sysIdOfYourGroup1');
qc.addOrCondition('group', 'sysIdOfYourGroup2');
qc.addOrCondition('group', 'sysIdOfYourGroup3');
ir.addQuery('user', person);
ir.query();
if(ir.next()) {
g_form.removeOption(<fieldName>, <choiceValue>);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2016 07:09 AM
You can just add or condition in the BR
if(gs.getUser().isMemberOf('mytestgroup1') || gs.getUser().isMemberOf('mytestgroup2') || gs.getUser().isMemberOf('mytestgroup3')){
g_scratchpad.isMember=true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2016 07:16 AM
Hi,
Try modifying the business rule as below.
- (function executeRule(current, previous /*null when async*/) {
- // Add your code here
- if(gs.getUser().isMemberOf('mytestgroup1'))
- g_scratchpad.isMember = gs.getUser().isMemberOf('mytestgroup1');
- else if(gs.getUser().isMemberOf('mytestgroup2'))
- g_scratchpad.isMember = gs.getUser().isMemberOf('mytestgroup2');
- else if(gs.getUser().isMemberOf('mytestgroup3'))
- g_scratchpad.isMember = gs.getUser().isMemberOf('mytestgroup3');
- })(current, previous);
This would check if the user is member of any one of the group, if the user user is a member it would set the scratch pad accordingly.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2016 07:20 AM
You could do this in just two lines. Why do you need if else if??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-30-2016 07:29 AM
Hi Abhinay,
I though it would be more effective to check if the user was a member of a group then no need to check for other 2 groups. But again I am not sure if script will evaluate to see if the user is member of all three groups if we give in single if statement.
Regards,
KN