Calling ServiceNow UI Actions via API PowerShell Script and Postman

Alex171
Mega Contributor

Hi All,

We have recently integrated ServiceNow with Octopus using PowerShell scripts that create a change request and force state changes to avoid the manual UI process. However this has caused issues (emergency changes error out and users who are in an approval group can approve their own change requests). I have been looking for a way to use the API to call UI actions and avoid the issue of forcing state changes.

I have followed the instructions I found from this link (https://servicenowthink.wordpress.com/2019/10/15/call-any-ui-action-with-rest-api-or-ajax-ootb-updated/):

There is only one endpoint to do POST call for UI actions.

POST @ /api/now/ui/ui_action/${sys_id_of_UI_action}
Additionaly, there are same 3 mandatory body parameters:

sysparm_table – table name of UI action.
sysparm_sys_id – sys_id of the record you want to run UI action against.
api=api (this has to be static).
If your UI action is designed to take additional parameters, you can pass them as well.

Note, if this is not working, you might need to set up Request header with Content-Type application/json and pass body parameters as following:

{
"fields": [ { "name": "" } ]
}
Also, if your UI action contains client side only code, like g_form, it has to be converted to server side for this to work, because without user session being present, g_form and other client side API’s will be equal to null.

When following these instructions exactly, I receive:

{
    "error": {
        "detail": null,
        "message": "Requested URI does not represent any resource: /now/ui/ui_action/"
    },
    "status": "failure"
}
 
When I remove the api value completely, and add our dev service account to the Authorization section, i receive:
 
{
    "error": {
        "type": "invalid_record",
        "detail": "Invalid value on Update",
        "message": "Invalid value on Update"
    },
    "status": "failure"
}
A few questions:
 
--I am not 100% sure I am pulling the correct sys IDs for the UI actions (I right click on the button, such as Save Draft or Update, and "Copy URL")
--I included the content type to be application/json. In the suggestion, do you know what might be included for the body parameters below?
 
{
"fields": [ { "name": "" } ]
 
Thanks,
Alex
1 ACCEPTED SOLUTION

Alex171
Mega Contributor

We brought in a web developer to assist and he was able to use the admin side to write up a custom script/rule to stop the Requested By user from approving their own changes if they are a member of the approver group. 

As I mentioned, this normally works out of box, but because we are using the API to force state changes from a PowerShell in Octopus, this breaks that rule. This custom script prevents that.

View solution in original post

6 REPLIES 6

Aoife Lucas
Giga Expert

I would not call a UI Action via REST, as it requires the UI be loaded to work, typically.  REST has no UI.  How does Octopus integrate?  Is it using REST or screen scraping?

Tapadh leat,

Aoife

Hi Aoife,

Here is a sample of code using REST API in a PowerShell script to create a SN change request. We borrowed from the REST API Explorer in SN.

 

Add-Type -AssemblyName PresentationCore,PresentationFramework

## Variables -- Only the state change variables are listed here
$state = "-4"
$implementState = "0"

## Authorization & Headers ##
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$headers.Add('Content-Type','application/json')

## Set Methods ##
$methodGet = "get"
$methodPost = "post"
$methodPatch = "patch"

----skipping past other part of the script doing verification----

## SN Script to create new Change request ##
$uriNewChange = "$SNWebURL/api/now/table/change_request?sysparm_display_value=true&sysparm_input_display_value=true"
$bodyNewChange = @{
type = $type
requested_by = "$requestedby"
category = $category
u_subcategory = $subcategory
priority = $priority
risk = $risk
impact = $impact
assignment_group = $assignmentgroup
u_change_manager_group_approval = $changemanagergroupapproval
u_cab_group_approval = $cabgroupapproval
short_description = $shortdescription
description = $description
implementation_plan = $implementationplan
backout_plan = $backoutplan
requested_by_date = $plannedStart
start_date = $plannedStart
end_date = $plannedEnd
state = $state
}
$bodyJsonNewChange = $bodyNewChange | ConvertTo-Json

## Get info from new SN Request created above ##
$responseNewChange = (Invoke-RestMethod -Headers $headers -Method $methodPost -Uri $uriNewChange -Body $bodyJsonNewChange -ContentType "application/json").result
$responseNewChange.number

RAHUL Khanna1
Mega Guru
Open the UI action record and right click on header and click on copy sysid and cross check did you copybthe same sysid

Hi Rahul,

I am still new to ServiceNow, and my primary role is in assisting integration, not managing the admin side of ServiceNow. I am not sure where the UI action record would be--is this only an Admin function?

When I am on an open Change Request and I right click on a button, I do get the "Copy URL" option, but it seems to be the same URL for each button/link.