Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Community Alums
Not applicable

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 @Community Alums ,

 

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
Giga Sage
Giga Sage

Hi 

If "employee_name" is reference variable. Try this

userGroupGR.addQuery("user", userName);

Moin Kazi
Kilo Sage
Kilo Sage

Hi @Community Alums ,

 

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

Community Alums
Not applicable

This worked!

 

Thank you Moin Kazi.