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

maybe i have to check my query that's being set in the client state.   because i made a new data broker that executes immediately, but with no filters.  And it's succeed fetch event is firing a debug client script with logging:

 

 

console.log('debuging');

console.log('debuging test data lookup: ' + JSON.stringify(api.data.look_up_multiple_records_1.results));

 

debuging

VM411:13 debuging test data lookup: [{"_row_data":{"displayValue":"4.3_2_modify-label-names.png","uniqueValue":"003a3ef24ff1120031577d2ca310c74b"},"file_name":{"value":"4.3_2_modify-label-names.png","displayValue":"4.3_2_modify-label-names.png"},"sys_id":{"value":"003a3ef24ff1120031577d2ca310c74b","displayValue":"003a3ef24ff1120031577d2ca310c74b"}}]

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