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

MrMuhammad
Giga Sage

Rather than adding users email id. You need to push user ids of users or groups. 

answer.push(appRec.sys_id.toString())

 

Regards,
Muhammad

Hi Madhu, Did you get a chance to try above? 

Regards,
Muhammad

Yes checked,.as the table is customed and the email address field is just a string field which stores the approvers mail address with comma separated value. How to get the approval goes to all those email address?

Hi Madhu,

So you have comma separated email addresses in that u_emailaddress field

for approvals to work it requires user sys_ids or group sys_ids

so you need to query sys_user table for those email addresses and then set store the user sys_id in the answer array one by one

// 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());

}

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