select random user from group

Deepthi13
Tera Expert

i want to select random user from a hard coded group, pls find below logic and suggest.

if it goes inside if it should consider approverList , if it goes else condition it should consider hard coded group and it should send approve randomly for any one person in group

 

  if (approverList.length == 0) {
approverList.push(gs.getUserID());
return approverList;
} else {
var group = new GlideRecord("sys_user_group");
group.addQuery("sys_id", "d0b0871adbf45780a167d6fa4b9619a0");
group.query();
if (group.next()) {
i=0;
for( )
return group;
}
}   

15 REPLIES 15

Can you keep your cursor over the activity & check for the error message.

In addition, were you able to print 

gs.info("grouplast: " + group.getRowCount()+ " - " + group.group.getDisplayValue());

in logs?

 

asifnoor
Kilo Patron

Hi,

Try like this. Also the table should be sys_user_grmember to selelct a user from it.

if (approverList.length == 0) {
  approverList.push(gs.getUserID());  
  return approverList;
} else {
  var group = new GlideRecord("sys_user_grmember");
  group.addQuery("group", "d0b0871adbf45780a167d6fa4b9619a0");
  group.query();
  var arr = [];
  while(group.next()) { 
     arr.push(group.user);
  }
  var random_index = arr[Math.floor(Math.random() * arr.length)];
  return arr[random_index];
}

Hi Deepthi,

Did you try the code that i have provided?

can i know y workflow it self getting cancelled? my script is called in workflow activity 

find_real_file.png

Hi

In the workflow, check if the approval users are created correctly? Go to the record and check the approvals and see if the users are assigned or not.