How to retrieve data with a Script Include in UI Builder?

gjz
Mega Sage

I'm really new to UI Builder and have a business requirement that I need help with.

 

Currently, in the core UI forms for catalog tasks (SCTASK), we have a UI Macro on the form that provides a list of consolidated work notes and user comments for the entire request so none get missed when fulfilling a request.  The UI macro calls a script include that gathers the data based on two parameters passed - the SCTASK sysid and the RITM sysid.

 

I need the same functionality for Service Operations Workspace and I'm stuck on how to create a data resource to call the script include.  I assume I can pass parameters to the data resource with a client state parameter or field value from the form controller, I just don't know how to do that in a data resource, or even what kind of data resource since I want to re-use the script include code.

 

Can someone help me out, please?

1 ACCEPTED SOLUTION

gjz
Mega Sage

It turns out you can call a Script Include from UI Builder, and it was easy and did not require a catalog client script or REST API.  It may not be efficient, but it works.

 

Here is how I did it:

Business requirement: Display a consolidate list of work notes and comments for a catalog task in the side bar of a standard record page that is open on a catalog task.  

 

1. On the standard record page of the workspace I'm using, I created a new Transform data resource.

2. In the data resource, I wrote the script I needed to get the data.  Once I had this working and it returned data, I 

3. Copied the script into a new function in an existing Script Include that is client callable.

4. Changed the Transform to call the script include the same way you normally would.  For those who are visual, this is what it looked like:

Transform Script Include.png

 

data resource.png

 

I still have to hook it up to the sidebar, add events to refresh data, limit it to only being used for a catalog task record, etc., but I have data!

View solution in original post

11 REPLIES 11

bonjarney
Mega Contributor
UI Builder doesn't have a "Script" data resource type — you can't run server-side GlideRecord code directly from a UIB page. You need a Scripted REST API as the intermediary between your Script Include and UI Builder.

The architecture: `Script Include → Scripted REST API → REST Data Resource in UIB → Component`

UI Builder does have a 'script' data resource type - Transform.  I'm not sure about the others, but this one does allow you to create scripts.  According to the Advanced training on NowLearning, it is "to transform the returned data output into a specific format".   That is what I needed to do and it worked great.

pr8172510
Giga Guru

Hi gjz,

Good question — this is a very common challenge when moving from classic UI (UI Macro) to UI Builder / Workspace.


1. Key point (why you're stuck)

In UI Builder:

  • You cannot directly call a Script Include like you do in UI Macros
  • UI Builder works through Data Resources (APIs)

 So your Script Include must be exposed via an API first


2. Recommended approach → Wrap Script Include in Scripted REST API (best practice)

This is the clean and supported way.


Step 1: Create Scripted REST API

  • Go to: System Web Services → Scripted REST APIs
  • Create a new API + Resource

Example:

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

var sctask = request.queryParams.sctask;
var ritm = request.queryParams.ritm;

var result = new MyScriptInclude().getConsolidatedNotes(sctask, ritm);

response.setBody(result);

})(request, response);

Step 2: Reuse your Script Include

Make sure your Script Include:

  • Is Server-side
  • Has a callable function     
    var MyScriptInclude = Class.create();
    MyScriptInclude.prototype = {
    getConsolidatedNotes: function(sctask, ritm) {
    // your existing logic
    }
    };   

     

    3. Step 3: Create Data Resource in UI Builder

    In UI Builder:

    • Add Data Resource → REST API
    • Configure:
      • Method: GET
      • Endpoint: your scripted API

    Pass parameters like:

     

     
    /api/x_your_scope/your_api? sctask=${state.sctask_sys_id}&ritm=${state.ritm_sys_id}
     

    4. Passing parameters (important)

    You were right — use:

    Client State Parameters
    ✔ OR Record fields (from form context)

    Example:

    • @context.record.sys_id → SCTASK
    • @context.record.request_item → RITM

    5. Bind data to component

    Once Data Resource is set:

    • Bind response to:
      • Data Table
      • Text / List component
      • Custom component

    6. Alternative (not recommended)

    You can use:

    • GlideAjax (client script style)

    But:

     Not aligned with UI Builder architecture
    Harder to maintain


    7. Best practice

    ✔ Use Scripted REST API as wrapper
    ✔ Keep Script Include reusable (as you already have )
    ✔ Use Client State for dynamic params

I like the response, and I've marked it helpful and bookmarked it for future use.  However, I was able to return data from my script include using a Transform Data Resource and didn't have to use the REST API.  I really appreciate the details, which is why I have bookmarked it.  I'm sure I will need this information some day.