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

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.

tomashrobarik
Giga Guru

Hi, you could create a new transform data resource for that. I highly recommend to have a look at some overview of how data resources work https://developer.servicenow.com/blog.do?p=/post/quebec-ui-builder-data-resources/

Thanks for your answer. Do you have a code example how to call the script include via transform data resource? I tried but no success.

First you need to have a transform data broker script, in which you have a script field.

function transform(input) {
//put any server side logic here

}

If you need to pass some input parameters you need to specify them within properties field. Just have a look at some OOTB examples. It is just a JSON. If you are updating/creating/deleting some gliderecord than don't forget to check mutates server data checkbox.

You also need to create an ACL for this data resource.

After that you can add that data resource on the page and execute it within the client script:

function handler({api, event, helpers, imports}) {
   
    
    api.data.id_of_your_data_resource.execute();
}

you can also pass some input parameters into the data resource by passing an object with appropriate key,value pairs.