Wanted to check "type" field from sys_user_group table using if activity(script) in workflow

Community Alums
Not applicable

Wanted to check group type using if activity in workflow

this is a workflow for a requested item. The If conditions depend on the field value which refer to group table, i want to check only two group types either it is Support or Finance 

Any suggestions on how to correct code below..

 

answer = ifScript();

function ifScript() {

var usrObj = new GlideRecord('sys_user_group');
usrObj.get(current.variables.cat_item_assignment_group);
if (usrObj.type == 'Support') {
return 'yes';
} else {
return 'no';
}
}

 

1 ACCEPTED SOLUTION

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

replace

 if (usrObj.type == 'Support') {

With

if (usrObj.type.getDisplayValue() == 'Support' || usrObj.type.getDisplayValue() == 'Finance') {

-Anurag

-Anurag

View solution in original post

5 REPLIES 5

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Type on the Group record, is a reference field to "sys_user_group_type". You are actually now checking on type == 'Support', while this should be a sys_id.

So change your if to check a sys_id, or change your script to check the type.name.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

For example, if I run below code, the result is a sys_id. Not the value "Support" or "Finance":

var usrObj = new GlideRecord('sys_user_group');
usrObj.get('b85d44954a3623120004689b2d5dd60a');
	
gs.info(usrObj.type);
*** Script: 1cb8ab9bff500200158bffffffffff62

To check either Support or Finance, indeed like Anurag mentioned:

usrObj.type == 'Support' || usrObj.type == 'Finance'

Though again, this should be sys_ids or type.name

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

replace

 if (usrObj.type == 'Support') {

With

if (usrObj.type.getDisplayValue() == 'Support' || usrObj.type.getDisplayValue() == 'Finance') {

-Anurag

-Anurag

AbhishekGardade
Giga Sage

Why are you even using GlideRecord Here ? You can simply dot-walk the fields and check it type as below

Note : Type field on group table is LIST Type and it also return the sys_id and you need to compare with sysid of the type selected. So Passing names like Support and Finance wont work here.

Your code will be like:

answer = ifScript();

function ifScript() {

var groupType = current.variables.cat_item_assignment_group.type ;

// If string not found, it returns -1 because 0 is the first position.

if (groupType .indexOf("sys id of support") != -1 || groupType .indexOf("sys id of finance") != -1 ) {

return 'yes';

} else {

return 'no';

}

 

}

Please mark as Correct Answer and Helpful, if applicable.
Thank You!
Abhishek Gardade

 

Thank you,
Abhishek Gardade