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 Devina,

Go to System policy-> Events-> event log and check if the events are triggered or not. If triggered check the state of the events.

 

Hi Asifnoor,

The events are triggered but its state is ready.

it should be in processed state right?

 

Thanks

Devina

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.

Hi Asifnoor,

the event Processing duration is showing 0.

I think the scipt action is not running. 😞

 

 

I assume the issue is resolved. Do let me know if you are facing any other issue. Kindly mark my other comment(s) as helpful if they are helpful.