UI Action executed before the Client Script

AleksandarJ
Tera Contributor

Hi everyone,

I have a situation where the current solution is not always working. I have a client script that onChange copies the value from one (first) field to another (second) . And, an UI action that is passing these field values in a script include, where they are later processed. After changing the value in the first field, the customer does not want to click anywhere else before clicking the UI action. This leads to situations where the sync process is execued before and sometimes after the UI action, and this results in incorrect parameters passed to the script include. Does anyone know if it is possible to "trigger" the client script before the execution of the UI action? The current workaround is done by inserting timeout in the UI action, but I would preffer not to be dependent from any waiting conditions.

Thank you.

Best regards,

Aleksandar

 

3 REPLIES 3

Yashsvi
Kilo Sage

Hi @AleksandarJ

 

To address the issue where a user changes a field value and immediately clicks a UI action in ServiceNow, leading to inconsistent data being passed to the backend, you can follow a structured approach to ensure data consistency. Here’s how you can achieve this without relying on timeouts:

 

Understanding the Problem:

When a user updates a field and clicks a UI action without clicking elsewhere on the form, the onChange client script might not complete its execution before the UI action starts. This can lead to scenarios where the UI action processes outdated or incorrect values.

 

Proposed Solution:-
1. Synchronize Client-Side Operations
Ensure that the onChange script's field update is completed before initiating the UI action. This involves explicitly managing the sequence of operations on the client side.

2. Using GlideAjax for Server Communication
Leverage `GlideAjax` to handle the communication between the client script and the server-side logic. By calling the server-side script include directly from the client script, you can ensure that the necessary field updates are performed before any server-side processing occurs.
Implementation Steps:

1.onChange Client Script Enhancement
- When the value of the first field changes, the client script should update the second field immediately.
- After setting the value, a custom function should be called to trigger the UI action logic.

2. Custom Function to Handle UI Action
- This function should gather the values of the relevant fields after they are updated.
- Use `GlideAjax` to call a script include, passing the updated field values.

3. Script Include for Server-Side Processing
- The script include processes the field values received from the client script.
- This ensures that the backend logic is always working with the most current data.

Key Concepts

- Client Script Execution Order: Ensure the field update logic runs to completion before any further actions are triggered.
- GlideAjax: A ServiceNow API that allows client-side scripts to communicate with server-side script includes. This is crucial for ensuring that data passed to the server is up-to-date.
- Script Include: Server-side scripts that contain reusable code logic. By calling these from the client script via GlideAjax, you can process data consistently and reliably.

Benefits of This Approach:

The core idea is to enhance the onChange client script to ensure it completes its field update before triggering any server-side processing. This is achieved by using a custom function that invokes the backend logic through GlideAjax. This method ensures that the UI action works with the correct and updated field values, maintaining data integrity and consistency. By structuring the client-server communication in this way, you can eliminate the dependency on timeouts and provide a more reliable user experience.

 

If this answer was helpful, I would appreciate if you marked it as such - thanks!

Lauri Arra
Tera Guru

Disclaimer: Since we don't know the full details of your setup, this might not be the ideal approach.

If the client script performs a very simple action, but you can't reliably make it happen before the UI Action triggering, you can consider changing the UI Action in a way that it always performs the same logic before passing the values forward. 

So that if you have field X, where onChange client script calculates new value to field Y and the UI Action uses fields X and Y as inputs, you could have the UI Action use only field X as input and the value for Y is calculated inside the UI action (or the script include it uses). 

Naturally this doesn't work if Y's value depends on the previous value in Y. Such case is probably not a good design and the field Y's logic could be re-evaluated.

newhand
Mega Sage

@AleksandarJ 
In the UI Action, do the things  what the client script do one more time .  

 

Please mark my answer as correct and helpful based on Impact.