We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

How to use POST requests in UI Builder

Paul Sisson
Tera Expert

First, create a Scripted REST Service (aka Scripted REST API or sys_ws_definition entry).

Within that record, create a Scripted REST Resource (sys_ws_operation) in the Resources related list.

The HTTP Method on that resource should be POST (obviously).

Here's an example script for that resource:

 

 

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
	var gr = new GlideRecord('u_scan_log');
       //note that to get your request body data, it's REQUEST.BODY.DATA
	var data = request.body.data;
	gs.log('data: => ' + JSON.stringify(data), "Paul");
	gs.log('response: => ' + JSON.stringify(response), "Paul");
	gr.initialize();
	gr.u_scan = data.u_serial;
	gr.u_message = data.u_message;
	gr.insert();

})(request, response);

 

 

Now, inside of an UI Builder client script, you can do something like this:

 

 

function handler({api, event, helpers, imports}) {
    helpers.snHttp("/api/hesi/depot_workstation/addScanLog", {
        method: "POST",
       // NOTE: you must JSON.stringify() the body.
        body: JSON.stringify({u_message: 'this is the message', u_scan: '2983599'}),
    })
    .then(({ response: { result } }) => {
        console.log(`result from UI: ${result}`);
    });

}

 

 

 

 

0 REPLIES 0