UI Builder - simple record count

AnirudhKumar
Mega Sage
Mega Sage

Hello folks,

 

I'm trying something really simple here : on button click, show number of incidents in the table.

Oddly it only works on the second click, wondering why it doesn't the first time.

Here's what I've got:

 

-  a button and stylized text components

- a client state parameter called mytext bound to the Stylized text component

- a data source for Aggregation Query where I've set Table to incident

- a client script as shown below:

 

 

function handler({api, event, helpers, imports}) {
console.log('in');// appears on every click
api.data.aggregation_query_1.refresh();
api.setState('mytext', api.data.aggregation_query_1.output.data.GlideAggregate_Query[0].count);
}

 

- The button has an event handler tagged to the client script

 

Anybody has any ideas? 

1 REPLY 1

Nootan Bhat
Kilo Sage

Hi @AnirudhKumar 

If you are trying to throw notification on button click, using 'Add alert notification', it will take some time to get the data fetched and filled into the State parameter.

please use below code in your client script:

function handler({api, event, helpers, imports}) {
console.log('in');// appears on every click
api.data.aggregation_query_1.refresh();
api.setState('mytext', api.data.aggregation_query_1.output.data.GlideAggregate_Query[0].count);
api.emit("NOW_UXF_PAGE#ADD_NOTIFICATIONS", {
        items: [{
            id: "alert1",
            status: "critical",
            icon: "circle-check-outline",
            content: {
                type: "html",
                value: "<h4>" + "Count " + api.data.aggregation_query_1.output.data.GlideAggregate_Query[0].count +"<h4>"
            },
            action: {
                type: "dismiss"
            },
        }]
    });
}

Check this: UI Builder || Alert messages on custom pages - ServiceNow Community 

let me know if it helped.

 

Thanks,