2

I am working on an application in ServiceNow where I have the following UI:

service now UI

I have to make a POST request when Submit is pressed. Till now I have got the following lead to make a request.

You can use the native Fetch API to make arbitrary HTTP calls, or you can use the helpers.snHttp method if you are going to be making API calls to your own ServiceNow instance

The Fetch API looks like something that I need here. When I try to run the following script, I get an error.
Script:

function sendRequest() {
    var test = '';
    try{ fetch('https://dummyjson.com/products/1')
        .then(res => res.json())
        .then(json => test = json);
    }catch(e){return e;}
    return test;
}

Error
TypeError: fetch is not a function

I am not finding a way to get rid of this error. I am totally new to ServiceNow scripting. Is there anything I am missing out on?

update 1
enter image description here

JAMSHAID
  • 1,258
  • 9
  • 32

1 Answers1

0

Based on your screenshot you are working with UI Builder, which seems to be restricted by ServiceNow. That's why you can't use fetch, I think you have to use the following to make HTTP Requests

helpers
.snHttp("/api/now/table/incident", {
  method: "POST",
  body: {
    description: "Sample description",
    close_notes: "Sample close notes",
    order: "-1"
  }
})
.then(({ response }) => {
  // handle POST request response
})
.catch(({ error }) => {
  // handle POST request errors
});

Link to ServiceNow Documentation - helpers.snHttp

makim
  • 3,154
  • 4
  • 30
  • 49