Event trigger and script action in workflow not working as expected

Devina Khare
Kilo Contributor

Hi All,

I want to add a single user into multiple group through catalog item. For which I have an item on which the user name is selected and the multiple groups are selected through list collector. I want to add the name of the group into the description field on the approval that is sent. For that I have created a event that is triggered when every approval is sent, into the event I am passing the Requests sysid and the name of the group for which the approval is sent. After that there is action action that is calling the approval table and adding the group name into it from the event.

The script is working fine and I am getting the correct value but only if one group is selected onto the list collector. When there are multiple group selected either they get one of the group name or it does not take group name in description.

Below is the snippet of the code to sent the approval from workflow:

// Set the variable 'answer' to a comma-separated list of user ids and/or group ids or an array of user/group ids to add as approvers.
//
// For example:
// answer = [];
// answer.push('id1');
// answer.push('id2');
var answer = [];
var groups = current.variables.add_users_groups.toString();
var listArr = groups.split(',');

for (var i=0;i<listArr.length;++i) {
var group = new GlideRecord('sys_user_group');

group.addQuery('sys_id',listArr[i]);
group.query();
if(group.next()){
var mgrID = group.manager;
answer.push(mgrID.toString());//push manager
gs.eventQueue("description.addition",current,current.sys_id,group.name);
//answer.push(group.sys_id.toString());
//gs.sleep(20000);
}
}

 

Below is the script written in my script action:

var sysId = event.parm1.toString();
gs.log("RITM Sys id is "+sysId);
var glide = new GlideRecord("sysapproval_approver");
glide.addEncodedQuery('sysapproval='+sysId+'^state=requested^u_descriptionISEMPTY');
glide.query();
gs.log("Row count is "+glide.getRowCount());
while(glide.next()) {
gs.log("inside loop");
var grpName=event.parm2;
gs.log(grpName);
glide.setValue('u_description',event.parm2);
glide.update();

}

 

Can anyone help me so that all the approval sent have the name of the group for which it is sent?

 

Thanks

Devina

1 ACCEPTED SOLUTION

Hi,

Are they stuck from long ago or only few mins? If few mins, then could be due to gs.sleep. 

I think the better way to fix this is liek this

1. remove gs.sleep from script action

2. In the code, where you are triggering the event.. trigger it like this

// For example:
// answer = [];
// answer.push('id1');
// answer.push('id2');
var answer = [];
var groups = current.variables.add_users_groups.toString();
var listArr = groups.split(',');
var gdt = new GlideDateTime();
gdt.addSeconds(60);
for (var i=0;i<listArr.length;++i) {
var group = new GlideRecord('sys_user_group');
group.addQuery('sys_id',listArr[i]);
group.query();
if(group.next()){
var mgrID = group.manager;
answer.push(mgrID.toString());//push manager
gs.eventQueueScheduled("description.addition",current,current.sys_id,group.name,gdt);
//answer.push(group.sys_id.toString());
//gs.sleep(20000);
}
}

if stuck from long time, go to active transactions and check if anything got stuck and kill it.

Mark the comment(s) as helpful if they are helping to solve the problem.

View solution in original post

17 REPLIES 17

Hi Asifnoor,

 

Its not working, the event is not getting trigerred and even if it is triggered how will it know that which group name to be populated in which approval.

 

Thanks

Devina

Hi,

your event is not triggering, May be because you added run script activity after the approval and since it is waiting for approval to happen it did not trigger the event.

Let us keep the earlier code as is. Lets trigger it before itself and add a delay of 2 secs in the  script action for approvals to create first. 

Now coming to your 2nd question, you can compare the approver group with the group.name parameter in the Script action and can update the group name in that approval description.

Try this code in script action

gs.sleep(2000);
var sysId = event.parm1.toString();
gs.log("RITM Sys id is "+sysId);
var glide = new GlideRecord("sysapproval_approver");
glide.addEncodedQuery('sysapproval='+sysId+'^state=requested');
glide.query();
gs.log("Row count is "+glide.getRowCount());
while(glide.next()) {
  gs.log("inside loop");
  var grpName=event.parm2;
  //check if the manager of this approval belongs to this group or not.
  var currentUser = gs.getUser().getUserByID(glide.approver); 
  if(currentUser.isMemberOf(­event.parm2.toString())) {
    var group_name = event.parm2.toString();
    glide.setValue('u_description',group_name);
    glide.update();
  }
}

 

 

 

Hi Asifnoor,

 

Its only populating the group name into one approval record.

 

Thanks

Devina

Check how many events are triggered and how many approvals are there?

Also add your condition u_description is not empty. I think i removed that.

Hi Asifnoor,

I think all the events are not getting processed as now its not even populating for one approval.

Is there there a queue that needs to be cleared to run the events?

 

Thanks

Devina