If script in workflow to check if user is member of approval group

Ankita Gupte
Kilo Sage

Hi Experts,

 

We have checkbox variable on group table i.e. u_approval.

 

We want to have if script in workflow which will check if employee_name is member of group having u_approval as true. If yes the if check activity output should be yes orelse no.

 

I have written below script but inspite of employee being member of group having u_approval true the output is going to no.

 

answer = ifScript();

function ifScript() {
var userName = current.variables.employee_name;
var userGroupGR = new GlideRecord("sys_user_grmember");
userGroupGR.addQuery("user.name", userName);
userGroupGR.query();

while (userGroupGR.next()) {
var groupGR = new GlideRecord("sys_user_group");
if (groupGR.get(userGroupGR.group) && groupGR.u_approval) {
return "yes"; // Return "yes" if any group has u_approval true
}
}

return "no"; // Return "no" if no approved group is found
}

 

Please advice

 

1 ACCEPTED SOLUTION

Moin Kazi
Kilo Sage
Kilo Sage

Hi @Ankita Gupte ,

 

What is the type of employee_name field, if it is a reference field then your script will always return "no". In that case you have to modify your script like below -

answer = ifScript();

function ifScript() {
var userName = current.variables.employee_name;
var userGroupGR = new GlideRecord("sys_user_grmember");
userGroupGR.addQuery("user", userName);
userGroupGR.query();

while (userGroupGR.next()) {
var groupGR = new GlideRecord("sys_user_group");
if (groupGR.get(userGroupGR.group) && groupGR.u_approval) {
return "yes"; // Return "yes" if any group has u_approval true
}
}
return "no"; // Return "no" if no approved group is found
}

 

Hope this help you.

 

Regards

Moin

View solution in original post

3 REPLIES 3

Shruti
Mega Sage
Mega Sage

Hi 

If "employee_name" is reference variable. Try this

userGroupGR.addQuery("user", userName);

Moin Kazi
Kilo Sage
Kilo Sage

Hi @Ankita Gupte ,

 

What is the type of employee_name field, if it is a reference field then your script will always return "no". In that case you have to modify your script like below -

answer = ifScript();

function ifScript() {
var userName = current.variables.employee_name;
var userGroupGR = new GlideRecord("sys_user_grmember");
userGroupGR.addQuery("user", userName);
userGroupGR.query();

while (userGroupGR.next()) {
var groupGR = new GlideRecord("sys_user_group");
if (groupGR.get(userGroupGR.group) && groupGR.u_approval) {
return "yes"; // Return "yes" if any group has u_approval true
}
}
return "no"; // Return "no" if no approved group is found
}

 

Hope this help you.

 

Regards

Moin

Ankita Gupte
Kilo Sage

This worked!

 

Thank you Moin Kazi.