Call Script Include from UI Builder

Elena
Tera Contributor

Hi everyone,

 

is there a way to call server side code (script include) from a client script in UI Builder? The script include is client callable and I tried to call it via GlideAjax, but no success. 

 

Can anyone share some experiences regarding that topic?

Thanks.

1 ACCEPTED SOLUTION

Oleg
Mega Sage

You can use helpers.snHttp for example to get any information via HTTP request. The corresponding code example could look like

/**
 * @param {params} params
 * @param {api} params.api
 * @param {any} params.event
 * @param {any} params.imports
 */
function handler({api, event, helpers, imports}) {
    helpers.snHttp(
        "/api/now/table/incident/a83820b58f723300e7e16c7827bdeed2",
        {
            method: "GET"
        }
    ).then(({ response: { result } }) => {
        debugger;
        console.log(`number=${result.number}; short_description="${result.short_description}"`);
    });
}

or, using async/await which simplifies working with Promises, like the following

/**
 * @param {params} params
 * @param {api} params.api
 * @param {any} params.event
 * @param {any} params.imports
 */
async function handler({api, event, helpers, imports}) {
    const { response: { result } } = await helpers.snHttp(
        "/api/now/table/incident/a83820b58f723300e7e16c7827bdeed2",
        {
            method: "GET"
        }
    );
    console.log(`number=${result.number}; short_description="${result.short_description}"`);
}

So you can create simply Scripted REST API and call it for loading required information.

View solution in original post

6 REPLIES 6

Thanks for your reply. Your solution worked as well, but I preferred the solution of Oleg. 

Paul Sisson
Tera Expert

Can anyone in this thread provide an example POST request? I don't know where I'm going wrong.

function handler({api, event, helpers, imports}) {
    helpers.snHttp("/api/hesi/depot_workstation/addScanLog", {
        method: "POST",
        body: "this is the body",
    })
    .then(({ response: { result } }) => {
        debugger;
        console.log(`result from UI: ${result}`);
    });
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // implement resource here
	var gr = new GlideRecord('u_scan_log');
	gs.log('request: => ' + JSON.stringify(request.body), "Paul");
	gs.log('response: => ' + JSON.stringify(response), "Paul");
	gr.initialize();
	gr.u_scan = request.body.u_serial;
	gr.u_message = request.body.u_message;
	gr.insert();

})(request, response);