Dynamic Approval creation using approval activity in workflow. Facing issue!

Madhu mukherjee
Mega Expert

Hi,

Just encountered this simple question, how to set approvers dynamically based on oe catalog variable. I tried to modify the script in multiple ways but not able to fix it. So we have a custom table where we have stored all those servers. In the catalog item form, we have a select box variable that refers to that table. Now the approval has to go to Email address field of the same table stores the email Ids with comma separated value. I am using the below code in approval activity, but it's not triggering the approval but it's going inside while loop. Any idea why is it skipping the approval record and moving to the next stage?

 

find_real_file.png

 

 

I am using the below script in advanced script option:

find_real_file.png

 

 

Any help will really be appreciated.

Thanks & Regards,

Madhu

1 ACCEPTED SOLUTION

Hi,

why you are using current object at line 19 and 20

Do you want User Approvals for the email addresses? if you have group email addresses then are you saying you want approvals to go to the group members as well?

if yes then please use below script

// your top code as it is

var emailAddressList = appRec.u_emailaddress.toString();

var userRec = new GlideRecord('sys_user');
userRec.addQuery('email', 'IN', emailAddressList);
userRec.query();
while(userRec.next()){
answer.push(userRec.sys_id.toString());
}

// now push group members to array
var groupRec = new GlideRecord('sys_user_group');
groupRec.addQuery('email', 'IN', emailAddressList);
groupRec.query();
while(groupRec.next()){
var members = new GlideRecord('sys_user_grmember');
members.addQuery('group', groupRec.sys_id);
members.query();
while(members.next()){
answer.push(members.user.toString());
}
}


If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

I got your answer. Thank you ankur! But it's working. I am trying with below code:

find_real_file.png

 

Also in the table, I see some of the email address is group email address, any idea how to push group sys ids as well to the appproval if that's not found in user table?

Thanks a lot,

Madhu

Hi,

why you are using current object at line 19 and 20

Do you want User Approvals for the email addresses? if you have group email addresses then are you saying you want approvals to go to the group members as well?

if yes then please use below script

// your top code as it is

var emailAddressList = appRec.u_emailaddress.toString();

var userRec = new GlideRecord('sys_user');
userRec.addQuery('email', 'IN', emailAddressList);
userRec.query();
while(userRec.next()){
answer.push(userRec.sys_id.toString());
}

// now push group members to array
var groupRec = new GlideRecord('sys_user_group');
groupRec.addQuery('email', 'IN', emailAddressList);
groupRec.query();
while(groupRec.next()){
var members = new GlideRecord('sys_user_grmember');
members.addQuery('group', groupRec.sys_id);
members.query();
while(members.next()){
answer.push(members.user.toString());
}
}


If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Yes it worked. Thank you so much

Manoj Kumar16
Giga Guru

You are using an array to push the email ids. If you pass a comma separated value then it will be taken as one value in the array. Please use a different function which will split by commas and return the value. Push each value.

Ok, any sample code you have fo reference?