How to use script includes in UI Builder
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2023 12:05 AM
Recently I wanted to use a script include in UIB. To my surprise, I was not able to find any instruction of how to do it. After some time, I have figured it out but it was not a trivial thing. Hence, I decided to share a basic guide for those who, similarly to me, struggle with the lack of information on this topic.
My post will have three parts:
- Basic information about script includes in UIB
- Backend part: How to create a definition of a new script include
- Frontend part: How to call the script include in a UIB script
In the post, I will be using these shortcuts:
- UIB = UI Builder
- SI = script include
- ES = EcmaScript
- FE = frontend
1) Basics about the UIB script includes
The UIB SIs are different from the standard SIs:
- the latter ones run server side, whereas the former ones only run client side.
This means that:
- UIB SIs are stored in their own, separate table: [sys_ux_client_script_include]
- you are not able to call server side APIs in UIB SIs (such as GlideRecord etc.)
- on the other hand, since the UIB SIs run in your browser, you are not restricted to ES5 in them (in other words, you are free use the arrow functions, "let" and "const" keywords etc.)
2) Backend part: How to create a new SI definition
In the UX Client Script Includes table [sys_ux_client_script_include], create a new record.
The form will be looking like this:
You might wonder what is the difference between Name and API Name fields:
- Name = the "display name" of your script
- on the FE side, you will use it to add the SI into a UIB script
- API Name = the "system name" of your script
- on the FE side, you will use it to call the SI in the UIB script
- API Name is read-only field and will auto-populate
- the value consists of two dot-separated parts:
- the first one = your current application scope
- the second one = the value you have put into the Name field
- so, if you are in the global scope and the Name is "exampleSI", the API Name would be: global.exampleSI
However, the most important field is the Script field. You can see a prefilled include function there, looking like this: function include({imports}){}. Here is where all the magic happens. Whatever you put in the empty curly brackets will be executed in your browser after you call the SI.
For example, let´s say you have a SI like this:
function include({imports}){
console.log("Hello world");
return "The script was called";
}
After calling it, this SI will log "Hello world" to your browser console, while returning another string ("The script was called") that can be further used in the script in which the SI is called.
Of course, usually we do not want the SIs just to return some hard-coded value. Usually, we want the SI to return a value based on an input. So how can we do THAT?
Well, this is the tricky part. In order to be able to pass any input values to the SI, we need to define a function WITHIN the include function and return that inner function.
I will explain it on an example. For the sake of simplicity, let´s assume we want a SI that will work like this: it will take two numbers as arguments and it will return their sum.
Such a SI could look like this:
function include({imports}){
return addTwoNumbers;
function addTwoNumbers(num1, num2) {
return num1 + num2;
}
}
As you can see, the core of the SI is still the include function as it was in the previous example. Only now the include function is somewhat more complex: it returns the addTwoNumbers function which, on its part, returns the sum of its two arguments.
The complete definition of our SI could look something like this:
For our purposes, we have allowed this script to be called from any application scope, but you can restrict it to your scope only (using Accessible from field).
Now we have our SI defined and we can call it from the FE.
3) Frontend part: How to call the SI in a UIB script
Let´s assume that in the UIB we have already created a page and on that page we have a script called exampleFrontendScript (which is triggered, for example, by clicking a button).
Now, inside this exampleFrontendScript we want to call our exampleSI, so we would pass it two numbers and receive their sum.
In order to achieve that, you need to do two things:
- add the SI to your script
- call the SI in your script
Step 1: adding the SI
- open your UIB script
- in our case: exampleFrontendScript
- in Details section, click on Script includes
- click on Add a script include field and choose the SI you want to use
- in our case: exampleSI
- the scripts are displayed by their names
- i.e. by the value of the Name field in the SI definition
- after choosing the SI, click on Add button (don´t forget this, otherwise the SI won´t work!)
- now the SI should have been added and the Details section should look like this:
Step 2: calling the SI
- to call the SI in your script, use this formula:
- imports['<API Name of the SI>']()(args)
- in our case: imports['global.exampleSI']()(num1, num2)
- in other words:
- we call the SI by using its API Name
- i.e. the value from the API Name field in the SI definition
- the API Name is a string and it is inside square brackets
- the square brackets are followed by round brackets in order to call the include function
- the round brackets are followed by another round brackets in order to call the addTwoNumbers function and pass it the arguments
- in our example, the logic behind the formula works like this:
- imports['global.exampleSI']
- this would return the include function (without calling it)
- as a result, we would receive the object of the include function
- imports['global.exampleSI']()
- this would return include function AND call it
- since the include function itself returns the addTwoNumbers function, this would return the addTwoNumbers function (without calling it)
- as a result, we would receive the object of the addTwoNumbers function
- imports['global.exampleSI']()(1, 2)
- this calls the addTwoNumbers function, passing it two arguments (1, 2)
- as a result, we receive the sum of the arguments => 3
So, the whole exampleFrontendScript can look something like this:
function exampleFrontendScript({api, event, helpers, imports}) {
let num1 = 1,
num2 = 2,
valueFromSI = imports['global.exampleSI']()(num1,num2);
console.log(`${num1} + ${num2} = ${valueFromSI}`);
}
I hope this post was useful. Any remarks, suggestions or further thoughts would be appreciated as I am by no means any expert on this topic.
- 14,221 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2024 08:26 PM
Thank you so much for this. I can't understand for the life of me why this isn't in the docs. Learning UI Builder for the first time these last two weeks has been an absolute nightmare and SN should be ashamed of their documentation on this part of the platform.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-22-2025 10:12 PM
Hi @HonzaProkes,
Thanks for your detail explanation about the "sys_ux_client_script_include".
I want some clarity about this can you please help on this.
I’ve created a form in UI Builder with two fields: URL and Short Description.
My requirement is:
When the user enters a URL and submits the form, it should check whether that URL already exists in a custom table.
If the URL already exists, the system should display an alert message:
"The URL already exists. Please enter a unique URL."
If the URL is unique, no alert is required and the submission should proceed as normal.
What is the best way to implement this logic in UI Builder?
Can you please provide the relevant processor and the script how to call the script include in UI builder if possible can you please provide the script also it will helpful for me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2025 12:28 AM
Hello, @shaik mahaboobj ,
for the use case you describe, the UIB script includes won´t help you. As I mentioned in the article, these scripts execute in your browser, they are purely front-end scripts without access to server-side APIs and data (tables).
For connecting your front-end solution with the back-end (e.g. tables), there are intermediaries called "data resources". For your use case, I guess you might leverage either Transform, or REST data resource; both will provide you with an option to execute scripts on server side. Please, check the following links:
- Data Resources (overview)
- Transforms
- REST
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2025 03:20 AM - edited ‎07-23-2025 06:17 AM
Hi @HonzaProkes,
Thanks for you quick response,
Could you please provide a step-by-step procedure for this? If possible, kindly include the necessary script as well. I do not have prior experience with UI Builder.
Thank's In Advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-23-2025 06:57 AM
Not the author but maybe I can help a little.
For starters you're going to need backend data for this, so you'll need to create a record on the sys_ux_data_broker_transform table:
- Name: how you'll find it in UI Builder as a "Data Resource"
- Application: Global or your scoped application
- Mutates server data: Yes if this code will update or create any records
- Private: Probably No
- Description: Whatever you want to put here
- Properties: ⇩
[{"name":"url","label":"URL","description":"","readOnly":"false","fieldType":"string","mandatory":true,"defaultValue":""}, {"name":"short_description","label":"Short Description","description":"","readOnly":"false","fieldType":"string","mandatory":false,"defaultValue":""}]
- Script: ⇩
// something along these lines
function transform (input) {
var url = input.url; //------------------------> These values comr from the name field in the Properties JSON array
var desc = input.short_description;
var gr = new GlideRecord('your_url_table_name');
gr.addQuery('your_url_field', url);
gr.query();
if (gr.next()) {
return JSON.stringify({ message: 'URL already exists in system, no records created', sys_id: null });
} else {
var newRecordGR = new GlideRecord('your_url_table_name');
newRecordGR.initialize();
newRecordGR.your_field_names = your_field_values;
// set values for your new record...
var sys_id = newRecordGR.insert();
if (sys_id) {
return JSON.stringify({ message: "Success", sys_id: sys_id });
} else {
return JSON.stringify({ message: "Failure to insert", sys_id: null });
}
}
Now onto what's next to make this work.
You're going to need to go into your UI Builder Experience Page, and add a Data Resource.
The Data Resource you add, should be the one you created above.
Use the Data Resource on the Submit event, and configure it with the state variables that hold the url and short description, as the labels show.
If you intend to pop an alert on the screen upon finding duplicate URLs:
- Create an Alert component
- Create a state variable called "alert_text"
- Configure the Alert component to be hidden unless alert_text has a legitimate value
- On operation success of the Data Resource, you should have a JSON value returned with a message and sys_id (which might be null)
- If JSON message value is "URL already exists in system, no records created", set the alert_text to whatever you want your alert to say, which will un-hide it