UI Builder client script is not running on page load

Jason M
Tera Contributor

Hi all,

I am working in UI Builder, trying to set up a client script to set the visibility of a few buttons on the form. I have my client script bound to the "Page Ready" event on the page so it will behave similarly to an onLoad client script in the traditional platform UI. I want to make a button become visible if the user logged in is the user that the record is assigned to.

I am in the process of setting up a client script to check some data and determine if the user should see the button, but the script will not run on the "Page Ready" event. My client script is as follows: 

find_real_file.png

The script does not log anything to the console when the page loads. I have this same script bound to a button, and when the page is not in the process of loading and I click the button, it logs the data to the console properly.

I have also set up a second client script just to test the Page Ready event, as follows:

find_real_file.png

I have this script bound to the Page Ready event as well and this one does work as expected, where it logs "hello is this on" to the console twice when the page loads. So it seems that maybe the look up record data is not available yet when the page is loading. But then how can I make the buttons visible or not visible depending on data when the page loads? Any advice would be much appreciated, thanks!

1 ACCEPTED SOLUTION

Brad Tilton
ServiceNow Employee
ServiceNow Employee

It sounds like the data isn't ready at page load. You could instead run this script on the data resource executed successfully (I don't think it's actually called that) event that fires when the data resource executes so you can be sure that the data has come back.

View solution in original post

4 REPLIES 4

vkachineni
Kilo Sage
Kilo Sage

can you do a and see if you get anything.

console.log(JSON.stringify(api.data));
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Hi,

Thanks for the suggestion. I tried this and got an empty object {} when running the script through the Page Ready event, as well as when running the script through a button.

Out of curiosity I also ran the script with 

console.log(JSON.stringify(api));

and it returned an object that had a property called data, again with an empty object {}. It looked something like this:

console.log(JSON.stringify(api));

{
  'viewports' : ...,
  'context': ...,
  'user': ...,
  'data': {},
}

 

Interestingly, the "state" section of the api also only showed one of my two client state parameters. I'm not sure if that's relevant but it's something I noticed.

Brad Tilton
ServiceNow Employee
ServiceNow Employee

It sounds like the data isn't ready at page load. You could instead run this script on the data resource executed successfully (I don't think it's actually called that) event that fires when the data resource executes so you can be sure that the data has come back.

We were able to get our requirement completed this way. I didn't realize that there was an event that fired when the data resource has finished executing but that was exactly what we were looking for. Thanks!