options in a field based on other field

MARISA INACIO1
Tera Contributor

Hello everyone

 

I have one field populated with the department of the logged in user and I have another List collector field with all users.
I would like to know how I can do the following. If I manually change the department, in the other users field I just want to be shown to choose users that belong to the department I indicate in the other field.

 

Thanks in advance

 

1 ACCEPTED SOLUTION

Also If you only want to display users from Department to be populated then there is no need to write script include also.

You just need to update reference qualifier on User Variable

javascript: "department.sys_idIN" + current.variables.department

you can replace 'current.variables.<with your variable name>'

 

VishalBirajdar7_0-1676549385784.png

 

 

Hope this will work for you...!!

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

5 REPLIES 5

Rajesh Chopade1
Mega Sage

Hi @MARISA INACIO1 

 

As per my understanding you want to populate group values or group members based on select group, if yes you can refer following links to resolve your issue.

https://community.servicenow.com/community?id=community_question&sys_id=f303339cdbc6b340feb1a851ca96... 

https://community.servicenow.com/community?id=community_question&sys_id=148ce6dddbf45c90190dfb243996... 

 

Thank you

Rajesh.

Hello @MARISA INACIO1 

 

You can write a client callable script include to get sys ids of group members and call that script include in reference qualifier of User field.

 

Step 1: Create Client callable script include and write below function in it

 

Script include Name : getUserUtils

client callable : yes

 

getUsersList: function(groups) {
var groupMembers;
var grGrpMem = new GlideRecord("sys_user_grmember");
grGrpMem.addEncodedQuery('group.sys_idIN' + groups);
grGrpMem.query();
while (grGrpMem.next()) {
groupMembers = groupMembers +','+ grGrpMem.getValue('user').toString();

}
//gs.log('getUserUtils=' + "sys_idIN" + groupMembers );
return "sys_idIN" + groupMembers;

},

  

VishalBirajdar7_0-1676463144034.png

 

Step 2 : call that script include function in reference qualifier of User field

 

Reference qualifier  - javascript&colon; new getUserUtils().getUsersList(current.variables.groups);

Here you can use, current.variables.<name of your variable>

 

VishalBirajdar7_1-1676463322476.png

 

and If you want to display user's depending upon department field just change the reference qualifier

javascript&colon;

var query = new getUserUtils().getUsersList(current.variables.groups);
query = query + "^department=" + current.variables.department;
query;

 

so that user's from specific department will be available.

 

VishalBirajdar7_0-1676465040211.png

 

 

Here is the output :

VishalBirajdar7_2-1676463446696.png

 

VishalBirajdar7_3-1676463472997.png

 

 

 

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Thanks for the great explanation. 

 

I'm trying to adapt it because what I want is that deep inside the department that I choose (variable department), only the users of that department appear, and it's not working. I put the cmn_department table but could you help me adjust the rest of the script to this table?

Thanks again

@MARISA INACIO1  Sure

If possible can you please share your code please ?

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates