Need to add multiple groups to a client script and business rule

michael_zeller
Kilo Explorer

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);

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

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;


}


View solution in original post

11 REPLIES 11

zica
Giga Guru

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>); 

}


Abhinay Erra
Giga Sage

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;


}


karthiknagaramu
Kilo Sage

Hi,



Try modifying the business rule as below.



  1. (function executeRule(current, previous /*null when async*/) {  
  2.  
  3.   // Add your code here  
  4. if(gs.getUser().isMemberOf('mytestgroup1'))
  5. g_scratchpad.isMember = gs.getUser().isMemberOf('mytestgroup1');  
  6. else if(gs.getUser().isMemberOf('mytestgroup2'))
  7. g_scratchpad.isMember = gs.getUser().isMemberOf('mytestgroup2');  
  8. else if(gs.getUser().isMemberOf('mytestgroup3'))
  9. g_scratchpad.isMember = gs.getUser().isMemberOf('mytestgroup3');  
  10. })(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.


You could do this in just two lines. Why do you need if else if??


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