Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Fetch users from a group with Rest API?

ErezA
Kilo Contributor

Hello,

I'm trying to fetch users list that belongs to a selected group and filter by username.

I haven't found a way to do it (unless fetch all group users and then fetch each user and then filter by names).

 

Can you guide me for a way to do it, please?

thanks

1 ACCEPTED SOLUTION

As far as I know the REST API only returns information on the fields that exist on that table. So, in this case you could get the list of users within the a specific group or groups and then send another call to the sys_user table to get more information from the user record. Or if you simply need the actual name of the user you can set the sysparm_display_value parameter to "all" and it will give you the display value and the sys_id of the user.

To filter based on user name (I'm assuming the user id/login id) then you would set that in the sysparm_query parameter like so: sysparm_query=user.user_nameLIKEchris

For example let's say I need to pull the users from the Service Desk group that have a "W" in their user_name and only bring back the user and group fields with display value and sys_ids using the REST Table API, the end point would look like this:

https:// <instance-name>.service-now.com/api/now/table/sys_user_grmember?sysparm_query=group.name=Service%20Desk^user.user_nameLIKEW&sysparm_display_value=all&sysparm_fields=user,group

The response would give me this from a PDI with OOB Demo Data:

{
  "result": [
    {
      "user": {
        "display_value": "Howard Johnson",
        "link": "https://<instnace-name>.service-now.com/api/now/table/sys_user/46ca0887a9fe19810191e08e51927ebf",
        "value": "46ca0887a9fe19810191e08e51927ebf"
      },
      "group": {
        "display_value": "Service Desk",
        "link": "https://<instnace-name>.service-now.com/api/now/table/sys_user_group/d625dccec0a8016700a222a0f7900d06",
        "value": "d625dccec0a8016700a222a0f7900d06"
      }
    },
    {
      "user": {
        "display_value": "Rob Woodbyrne",
        "link": "https://<instnace-name>.service-now.com/api/now/table/sys_user/8d5938070a0a0a6b00f8a5e8d3375606",
        "value": "8d5938070a0a0a6b00f8a5e8d3375606"
      },
      "group": {
        "display_value": "Service Desk",
        "link": "https://<instnace-name>.service-now.com/api/now/table/sys_user_group/d625dccec0a8016700a222a0f7900d06",
        "value": "d625dccec0a8016700a222a0f7900d06"
      }
    }
  ]
}

 

If you needed more information and need to also tie in the group and only want to do it in one call, then a Scripted REST API would be the better way to go than the OOB setup.

View solution in original post

4 REPLIES 4

ChrisBurks
Giga Sage

If using the Table REST API, a good table to fetch from is the sys_user_grmember table. This table will have reference to both the group and the user.

ErezA
Kilo Contributor

Thanks for replay

 

sys_user_grmember is returning only user sys_id, is it a way to return more user data (and also filer by user name)?

 

thanks

As far as I know the REST API only returns information on the fields that exist on that table. So, in this case you could get the list of users within the a specific group or groups and then send another call to the sys_user table to get more information from the user record. Or if you simply need the actual name of the user you can set the sysparm_display_value parameter to "all" and it will give you the display value and the sys_id of the user.

To filter based on user name (I'm assuming the user id/login id) then you would set that in the sysparm_query parameter like so: sysparm_query=user.user_nameLIKEchris

For example let's say I need to pull the users from the Service Desk group that have a "W" in their user_name and only bring back the user and group fields with display value and sys_ids using the REST Table API, the end point would look like this:

https:// <instance-name>.service-now.com/api/now/table/sys_user_grmember?sysparm_query=group.name=Service%20Desk^user.user_nameLIKEW&sysparm_display_value=all&sysparm_fields=user,group

The response would give me this from a PDI with OOB Demo Data:

{
  "result": [
    {
      "user": {
        "display_value": "Howard Johnson",
        "link": "https://<instnace-name>.service-now.com/api/now/table/sys_user/46ca0887a9fe19810191e08e51927ebf",
        "value": "46ca0887a9fe19810191e08e51927ebf"
      },
      "group": {
        "display_value": "Service Desk",
        "link": "https://<instnace-name>.service-now.com/api/now/table/sys_user_group/d625dccec0a8016700a222a0f7900d06",
        "value": "d625dccec0a8016700a222a0f7900d06"
      }
    },
    {
      "user": {
        "display_value": "Rob Woodbyrne",
        "link": "https://<instnace-name>.service-now.com/api/now/table/sys_user/8d5938070a0a0a6b00f8a5e8d3375606",
        "value": "8d5938070a0a0a6b00f8a5e8d3375606"
      },
      "group": {
        "display_value": "Service Desk",
        "link": "https://<instnace-name>.service-now.com/api/now/table/sys_user_group/d625dccec0a8016700a222a0f7900d06",
        "value": "d625dccec0a8016700a222a0f7900d06"
      }
    }
  ]
}

 

If you needed more information and need to also tie in the group and only want to do it in one call, then a Scripted REST API would be the better way to go than the OOB setup.

ErezA
Kilo Contributor

thanks for detailed replay. it will help for my needs