Association between user & group API

Adithya Khurana
Tera Contributor

What is the api to get the user and group association ? ( i.e sys_user_has_role contains the association between user and roles similarly is there anything for group )

2 ACCEPTED SOLUTIONS

priyasunku
Kilo Sage

@Adithya Khurana  sys_user_grmember table gives you the user and group relationship.

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful

 

View solution in original post

Prince Arora
Tera Sage
Tera Sage

@Adithya Khurana ,

 

Please try "sys_user_grmember";

 

API:  /api/now/table/sys_user_grmember/{Sys_id}

 

 

PRINCE_ARORA_0-1678442481033.png

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.

View solution in original post

4 REPLIES 4

SoniaShridhar13
Giga Guru

Hi @Adithya Khurana  

Please take a look at the below example and make a function call with the required role and sys_user Column name.The function getUsersWithRole should return you all the users who have the required role.

//Call the below function with the ROLE and one of the columns on sys_user table. //The below function call returns all the userNames of the users who have the role "Test_Role" var users = getUsersWithRole("Test_Role","user_name"); gs.print(users); //Array of Users with required role function getUsersWithRole(role,user_column) { var users = []; var userRoleJson = makeAPICall("https://***.service-now.com/api/now/table/sys_user_has_role?role="+role); for(var i=0; i<userRoleJson.result.length ;i++) { var usersJson = makeAPICall(userRoleJson.result[i].user.link); users.push(usersJson.result[user_column]); } return users; } function makeAPICall(endPoint) { try { var rest = new sn_ws.RESTMessageV2(); rest.setEndpoint(endPoint); rest.setHttpMethod("get"); //This below user needs to have access to sys_user_role table and sys_user table rest.setBasicAuth("Test_User","Test_123"); var response = rest.execute(); var responseBody = response.getBody(); var httpStatus = response.getStatusCode(); if(httpStatus==200){ return JSON.parse(responseBody); } } catch(ex) { var message = ex.getMessage(); } }

 

Please mark it helpful if it helps..

 

Thanks,

Sonia

Thanks. I want to know the api / table name for user & groups association.

shaunthesheep_0-1678442516948.png

Eg: userx is associated with 3 groups, what is an api to get those associations ? 

priyasunku
Kilo Sage

@Adithya Khurana  sys_user_grmember table gives you the user and group relationship.

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful

 

Prince Arora
Tera Sage
Tera Sage

@Adithya Khurana ,

 

Please try "sys_user_grmember";

 

API:  /api/now/table/sys_user_grmember/{Sys_id}

 

 

PRINCE_ARORA_0-1678442481033.png

 

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.