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

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.

RAHUL Khanna1
Mega Guru

If this is resolved, you can close the thread by marking suitable replies as helpful/Asnswered