NottyHead
NottyHead

Reputation: 187

Retrieve UserName from ServiceNow

I am able to retrieve records for a particular Incident ID using Invoke-RestMethod. However, while retrieving the data, values like Resolved To, Updated By, etc. get populated by a sysid.

Resolved By comes in this format:

https<!>://devinstance.servicenow.com/api/sysid, value= sysid

I would like to view the username instead of the sysid.

Upvotes: 2

Views: 1696

Answers (1)

Tim Woodruff
Tim Woodruff

Reputation: 601

The 'User ID' (user_name) isn't on the Incident, it's on the sys_user table, so you'll have to dot-walk to it.

If you're using the table API, you'll need to specify a dot-walked field to return, using the sysparm_fields query parameter.

This is no problem, just specify your endpoint like this:

$uri = "https://YOUR_INSTANCE.service-now.com/api/now/table/incident?sysparm_query=number%3DINC0000001&sysparm_fields=resolved_by.user_name"

I've specified a query for a specific incident number is requested, but you can replace that with whatever your query is.The important part is sysparm_fields=resolved_by.user_name. You'll want to specify any other fields you need here, as well.

The JSON I get as a result of running this API call, is the following:

{
  "result": [
    {
      "resolved_by.user_name": "admin"
    }
  ]
}

Note the element name: "resolved_by.user_name".

Another option for doing this, would be to tell the API to return both display, and actual values by specifying the sysparm_display_value parameter and setting it to all to return both sys_id and display value, or just true to return only display values.

Your URI would then look like this:

https://dev12567.service-now.com/api/now/table/incident?sysparm_query=resolved_byISNOTEMPTY%5Enumber%3DINC0000001&sysparm_display_value=all

And your JSON would contain the following:

"number": {
   "display_value": "INC0000001",
   "value": "INC0000001"
 },
 "resolved_by": {
   "display_value": "System Administrator",
   "link": "https://YOUR_INSTANCE.service-now.com/api/now/table/sys_user/6816f79cc0a8016401c5a33be04be441",
   "value": "6816f79cc0a8016401c5a33be04be441"
 },
 "sys_updated_by": {
   "display_value": "admin",
   "value": "admin"
 },

This would be accessed by:

answer.result[n].resolved_by.display_value

Upvotes: 4

Related Questions