UI Builder - simple record count

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2023 04:00 PM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-02-2023 01:31 AM
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,