- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Client Scripts in UI Builder
Table of Contents
- What Are Client Scripts in UI Builder
- Scenario 1: Button Press Displays Alert
- Step 1. Open UI Builder
- Step 2: Create a Page
- Step 3: Add Button Component
- Step 4: Create a Client Script
- Step 5: Connect Functionality
- Save and Test
- Scenario 2: Button Press Toggles List
- Step 1. Open UI Builder
- Step 2: Create a Page
- Step 3: Add Button Component
- Step 4: Add a List Component
- Step 5: Create Client State Parameter
- Step 6: Bind Parameter
- Step 7: Create a Client Script
- Step 8: Connect Functionality
- Save and Test
- Conclusion
Articles Hub
Want to see all of our other articles and blogs related to UI Builder and Workspaces?
You can copy the link above and share it!
Overview
Prerequisites
Before starting this tutorial, ensure you've completed the necessary prerequisites to follow along smoothly. Here are some key articles you might need to reference:
- How to make a workspace from scratch: In this article, we created the Customer Support Workspace.
Troubleshooting
If at any point something isn't working, try clearing the UI Builder cache.
Within your page variant editor in UI builder, click the hamburger menu icon at the top-left, go to Developer > Clear UI Builder Cache. You can also try clearing your local browser cache.
What Are Client Scripts in UI Builder?
g_form
to interact with the form, whereas UI Builder client scripts have access to the page’s context, including client state parameters, page properties, and data resources on the page.Scenario 1: Button Press Displays Alert
Step 1. Open UI Builder
- In your ServiceNow instance, use the Application Navigator to go to Now Experience Framework > UI Builder.
- When UI Builder opens, you should see the UI Builder Home screen.
- Select Experiences Tab at the top of the screen.
Step 2: Create a Page
- In the Experience Editor, find Pages and variants.
- Click the + icon next to it.
3. Select Create a new page.
4. Select Create from scratch instead.
Step 3: Add Button Component
- Under the content tree, select '+ Add content'.
- Then search for Button in the component search pop up.
- Select the Button component.
Step 4: Create a Client Script
1. Select + to the right of Client scripts, underneath the Data and scripts section, on the bottom left side of page.
/**
* @param {params} params
* @param {api} params.api
* @param {any} params.event
* @param {any} params.imports
* @param {ApiHelpers} params.helpers
*/
function handler({
api,
event,
helpers,
imports
}) {
api.emit("NOW_UXF_PAGE#ADD_NOTIFICATIONS", {
items: [
{
id: "alert1",
status: "critical",
icon: "circle-exclamation-fill",
content: {
type: "string",
value: "Critical: System failure detected!"
},
action: { type: "dismiss" }
},
{
id: "alert2",
status: "high",
icon: "circle-exclamation-outline",
content: {
type: "string",
value: "High: CPU usage exceeded 90%"
},
action: { type: "dismiss" }
},
{
id: "alert3",
status: "moderate",
icon: "circle-pause-outline",
content: {
type: "string",
value: "Moderate: Disk space is below 20%"
},
action: { type: "dismiss" }
},
{
id: "alert4",
status: "warning",
icon: "triangle-exclamation-outline",
content: {
type: "string",
value: "Warning: Unusual login activity detected"
},
action: { type: "dismiss" }
},
{
id: "alert5",
status: "info",
icon: "circle-question-fill",
content: {
type: "string",
value: "Info: A new software update is available"
},
action: { type: "dismiss" }
},
{
id: "alert6",
status: "positive",
icon: "check-circle-outline",
content: {
type: "string",
value: "Positive: Backup completed successfully"
},
action: { type: "dismiss" }
},
{
id: "alert7",
status: "low",
icon: "bell-fill",
content: {
type: "string",
value: "Low: Minor update recommended"
},
action: { type: "dismiss" }
}
]
});
}
Step 5: Connect Functionality
- Underneath the content tree, Select the button we created: Showcase Alerts.
- In the configuration panel, Select the Events tab.
- Select Add handler underneath the Button clicked event.
- Search for alerts.
- In the Scripts section you will see the Alerts script we created, Select it.
- Select Continue.
- For When to trigger select Always.
Save and Test
- In the upper right corner of the page, select Save.
- Select Preview (to the left of Save).
Scenario 2: Button Press Toggles List
Step 1. Open UI Builder
- In your ServiceNow instance, use the Application Navigator to go to Now Experience Framework > UI Builder.
- When UI Builder opens, you should see the UI Builder Home screen.
- Select Experiences Tab at the top of the screen.
4. Open the Customer Support Workspace.
Step 2: Create a Page
- In the Experience Editor, find Pages and variants.
- Click the + icon next to it.
3. Select Create a new page.
4. Select Create from scratch instead.
Step 3: Add Button Component
- Under the content tree, select '+ Add content'.
- Then search for Button in the component search pop up.
- Select the Button component.
Step 4: Add a List Component
- In the Content Tree, Select the +Add Content button.
- Search for List, in the component search.
- Select the List component.
- In the content tree, select the menu dots to right of 'List 1' (they display after hovering over the List 1 text).
- Select Rename
Step 5: Create Client State Parameter
- On the bottom left of the page, underneath Data and scripts, select Client state parameters.
Step 6: Bind Parameter
2. In the Configuration panel, Select the Configure tab > Data > Table.
3. Hover over the Table field until you see the Data Bind icon (stacked circles) > Select the Data Bind icon.
Step 7: Create a Client Script
- In the bottom left corner of the page, select Data and scripts, then select Client scripts.
- Change the name of the script to Toggle Logic.
- Delete the prepopulated client script that appears.
- Copy then paste the script below, in its place.
/**
* @param {params} params
* @param {api} params.api
* @param {any} params.event
* @param {any} params.imports
* @param {ApiHelpers} params.helpers
*/
function handler({
api,
event,
helpers,
imports
}) {
// if client state parameter is incident
if (api.state.myTable == 'incident') {
// set client state parameter to 'change_request'
api.setState('myTable', 'change_request');
} else {
// else set client state parameter to 'incident'
api.setState('myTable', 'incident');
}
}
Step 8: Connect Functionality
2. Select Events tab on the configuration panel
3. Below Button clicked, Select Add Handler
4. Search for Toggle Logic (the name of the client script we created earlier)
5. In the Scripts section, select the script named Toggle Logic
6. Select Continue
7. Below When to trigger > Select Always
Save and Test
1. In the upper right corner of the page, select Save.
2. Select Preview (to the left of Save).
change_request
table, then after selecting the Table Toggle button, display records from the incident
table, as below:Conclusion
Congratulations! 🎉 With the fundamentals covered, you can continue to experiment in UI Builder. Try combining multiple events and scripts to create richer interactions. And be sure to consult the official docs and developer community for deeper dives into UI Builder Client Scripts!
If you found this guide helpful, consider sharing it with peers or teams looking to elevate their UI Builder skills. If you like this type of content, please mark the article "Helpful" leave us your topic ideas in the comments, and fill in the survey at the top!
Keep an eye out for future tutorials where we’ll dive deeper into crafting immersive, data-driven experiences. Until then—happy designing, and stay curious!
Check out the Next Experience Center of Excellence for more resources
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.