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

RAHUL Khanna1
Mega Guru

Can you try changing 

 

if(group.next()){ 

 

to 

while(group.next())

 

asifnoor
Kilo Patron

Hello Devina,

You are checking for description is empty, which will fail after the 1st group name is added into the description. Kindly remove that. Here is the updated script action code. Try this out.

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;
gs.log(grpName);
var group_name = glide.getValue("u_description");
group_name = group_name +", "+event.parm2.toString();
glide.setValue('u_description',group_name);
glide.update();
}

Mark the comment as a correct answer and also helpful if this solves the problem.

Hi Asifnoor,

 

 I don't want all the approval have the name of all the group that is why I was making description to be empty.

So as per my requirement when the first approval is triggered from the workflow event should be triggered and the script action should populate the group name comming from the event. Then that event should be processed.

Then when the second approval is triggered the script action should work on the second event record and populate only second group name into the second approval. And this way the process should go on.

But currently the script is only populating one group name from one event in all the approval records.

Also into the logs when one script action runs on first event the row count into the approval table is 0 and when it runs for second then its directly  2.

Can you tell me a way to solve it?

 

Thanks

Devina

Hi,

This is because the event is triggered first before the approvals are created.

May be you shall trigger this event after all approvals are created. Add a run script activity and check liek ths and remove the event triggering line from the earlier code.

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');
  if(group.get(listArr[i].toString())) {
    gs.eventQueue("description.addition",current,current.sys_id,group.name);
  }
}