The CreatorCon Call for Content is officially open! Get started here.

UI Builder - How to find record reference in another table (Look up records)

User303823
Tera Contributor

I am prototyping a cutom 'app' using workspace and am looking at ways to build page(s) using UIB to assist with search and selection of various records. I have several tables working to accomplish this, but to simplify for my question there are two main tables to consider: a 'templates' table and an m2m table which associates the selected template with a specific design. 

 

In UIB, I have configured a Look up records data resource for the templates table and am displaying these on the page using a repeater > card based container design. What I'm looking to achieve is to find a match between the template record result card (sys_id) and a record in my m2m table (if it exists). If found, I'd like to do something on my page (hide the card, display a highlighted value, etc). 

 

Outside of UIB, I'd just query the m2m table for the sys_id I'm looking for. However, within UIB I cannot seem to accomplish this task. I've looked through the formula builder and scripting API but have not had success yet. I thought I was close with the formula WHERE_ONEOF(array, path, options), but either it doesn't do what I think it does or I'm just unable to form the options correctly. 

 

Another way to ask this might be, how can we work with the records returned by a Look up records data resource outside of binding to a component?

 

Any examples or links that help me get closer to figuring out how to query if a sys_id returned in the results of one data resource exists in another set of results is appreciated.

 

 

10 REPLIES 10

 

This took quite a bit longer than I had hoped. Some odd data 'disconnects' kept creeping in and aftern a restart on the page, the following is what ended up working for me. Working in this case means setting a JSON object client state parameter with the data I need to present on my card (via repeater component). 

 

What is NOT working is binding this JSON array to the repeater. I have validated that the JSON is being populated with the expected data, but every attempt at the repeater binding fails. No data is available to me to render on the page/card. 

 

I'm wondering if it is the JSON object structure? My approach was very straightforward... set the repeater data array to my client state variable (JSON array). Then attempt to bind labels to the individual keys within the JSON client parameter. No luck. 

 

Has anyone had success with the array client parameter-to-data binding? Anything I'm missing? I'm close enough I'd like not to give up, but I end up spending so much time researching each step I may have to look into something else.

 

var all_records = api.data.look_up_multiple_records_1.results;   //all records
var selected_records = api.data.look_up_multiple_records_2.results;  //selected records (m2m table)

let rptr_object_json = [];

for (var i = 0; i < all_records.length; i++){
    //Build JSON object with data from one data resource and a boolean selected value set based on matching query 
    from another data resource
     rptr_object_json.push({
        sysid: all_records[i]._row_data.uniqueValue.toString(),
        id: all_records[i]._row_data.displayValue.toString(),
        type: all_records[i].u_subtype.displayValue.toString(),
        use: all_records[i].u_use.displayValue.toString(),
        selected: false //everything set to false at initial creation
     });
}
    for (var j = 0; j < selected_records.length; j++){
        var select = selected_records[j].u_template_record.value.toString();
        rptr_object_json.forEach(rec => {
            if (rec.sysid == select) //match of sys_id in both data resources 'all records' and 'selected records (m2m)'
                rec.selected = true; //set the selected key to true if match found in both data resources
        });
    }

    api.setState("allrecords_JSON", rptr_object_json);
    console.log("JSON Result | ", api.state.topology_JSON);

 

Kevin83
ServiceNow Employee
ServiceNow Employee

What does the binding look like on the repeater component? what does the binding look like on components within the repeater?

Here is the config of the items I'm working with... starting with the JSON object definition (client state).

 

Client State Config

User303823_0-1737475607733.png

I have defined the client state object with the same 'keys' as I'm setting with my client script. For what it's worth, I also tried leaving the object completely blank (same outcome). 

 

Repeater Component Config

User303823_1-1737475746145.png

Here I'm replacing the lookup_multiple_records data source with my client state variable. It only ever displays as 'empty' and never with the 11 values it should have (per console.log). 

 

Card Component Item Config

User303823_2-1737475886462.png

Now I'm attempting to bind one of the key+value pairs to a label within the card base container. As it appears the parent repeater component never receives any data from the client state parameter, my card items do not either.

 

I'm wondering if there is an expected data structure the repeater component requires? I attempted the suggestion from @IronPotato and inspected the card event.payload to see if it would be useful. I was able to see the object in the console but not make is useful in my work.

 

Still chipping away at this. Appreciate all who've piled onto this and assisted in my quest (and learning). 

Kevin83
ServiceNow Employee
ServiceNow Employee

If the card component is within the repeater you should see a repeater binding section. so I think you would need @item.value.id

 Do you ever see something like this?

Screenshot 2025-01-21 at 2.41.25 PM.png

When I have the repeater component set to the 'lookup_multiple_records' data source, then my card components (ex. label) DO have the repeater > value (@item.value) option. When I have it set to the Client State var I've created, this is not an option. The repeater option is not available. If you look back at my previous post's image (Card Component Config) you can see where it is missing. 

 

The client state parameter does not seem to be accessible by the repeater due to bad structure or something else missing.