UI Builder: How to access in a script the results of a data resource refresh

staticCrash
Tera Guru

Hello,

How do I access the results of a data resource fetch in a script?

 

Goal:

Is to fetch the sys_id of an attachment record for a given table sys_id.

With the sys_id of the attachment record,  I can send that to a page with a document viewer component.  

On this page a pdf will be rendered in the doc viewer.

 

Background:

I have a PDF file attached to a cmn_location record.

 

Data:

Sysid of location record:  c8a0f5f1839a921047326060ceaad300

Sysid of attachment record: 8f5175bd83d6921047326060ceaad39b

 

Components:

Simple list of the cmn_location table, with an on reference click calling client script

staticCrash_0-1733521803263.png

Client Script called  upon reference link clicked:

 

/**
* @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}) {
    
    var pl = event.payload;
api.setState('attQuery', 'table_sys_id=' + pl);
    
    
    api.data.getAtt.refresh();
   
    
    
}

 

 

Next is a data resource that looks up the attachment record using the client state attQuery that was set in the above script.

staticCrash_2-1733522378572.png

 

Upon successful fetch, this script needs to get the sys id that was returned by the query.  I can't seem to find this value.  So I hardcode a sys id.  and the page is redirected to a page with the doc viewer component. 

 

 

/**
* @Param {params} params
* @Param {api} params.api
* @Param {any} params.event
*/
function evaluateEvent({api, event}) {
	console.log('event payload: ' + event.payload);
    //@data.getAtt.results.0.sys_id.value
    console.log('data resource results: ' + api.data.getAtt.results[0].sys_id.value);
    
    var u = '8f5175bd83d6921047326060ceaad39b'; //debuging
       return {
		route: null,
        fields: null,
        params: null,
        redirect: null,
        passiveNavigation: null,
        title: null,
        multiInstField: null,
        targetRoute: null,
        external: {
            url: "/now/docviewer/viewer/" + u
        },
        navigationOptions: null
	};
}

 

 

@Data.getAtt.results.0.sys_id.value seemed to be a clue, but it's null, 

when i try to log it:   console.log('data resource results: ' + api.data.getAtt.results[0].sys_id.value);

it's null.  Also nothing is useful in the event payload as it's all private data, and has nothing for the results of the query.

 

Please advise.  

 

Thank you,

Jay

 

What the second page looks like when i hardcode the attachment sys id:

staticCrash_0-1733522971106.png

 

 

1 ACCEPTED SOLUTION

staticCrash
Tera Guru

After fixing my query in the client script, i was able to find where the results are stored.

right here, in my case:  api.data.getAtt.results[0].sys_id.value

 

The following logging in the page redirect event script helped me:

console.log('data resource results: ' + JSON.stringify(api.data.getAtt.results));

 console.log('att sysid: ' + api.data.getAtt.results[0].sys_id.value);

 

data resource results: [{"_row_data":{"displayValue":"TM-D710G_CD-ROM_English.pdf","uniqueValue":"8f5175bd83d6921047326060ceaad39b"},"file_name":{"value":"TM-D710G_CD-ROM_English.pdf","displayValue":"TM-D710G_CD-ROM_English.pdf"},"sys_id":{"value":"8f5175bd83d6921047326060ceaad39b","displayValue":"8f5175bd83d6921047326060ceaad39b"}}]

 

att sysid: 8f5175bd83d6921047326060ceaad39b

 

api.data.getAtt.results[0].sys_id.value

As you can see, the script syntax is very close to the data pill view of the data broker's preview:

@Data.getAtt.results.0.sys_id.value

View solution in original post

6 REPLIES 6

Kevin83
ServiceNow Employee
ServiceNow Employee

What data broker are using to fetch the data?

Can you try logging the value of "api.data.getAtt.result" instead of "api.data.getAtt.results"? 

Also what event handler are you using? "Data fetch suceeded"?




Hi Kevin,

Thanks for your help.  

Correct, Data fetch succeeded is the event handler.

The data broker is look up multi records, with a max results set to 1.

 

I can try your suggestion, however the pill view of the data recourse shows results.

staticCrash_0-1733839228413.png

 

Hi Kevin, Here are some console lines.

console.log('event payload: ' + JSON.stringify(event.payload));
       console.log('data resource results: ' + JSON.stringify(api.data.getAtt.results));
    console.log('data resource result: ' + JSON.stringify(api.data.getAtt.result));

event payload: {"dataElemId":"getAtt","__uxfCompositionElId":"af5031f1839a921047326060ceaad3d4"}
VM120:12 data resource results: []
VM120:13 data resource result: undefined

Thanks!

Jay

So it looks like the results array is empty upon succeed of fetch.

So the trick is when and where to grab the results from a data broker?

thanks