- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2024 02:57 PM - edited 11-21-2024 10:02 AM
Hi All,
I created the following Client Script to display an alert/popup message, whenever the problem record state changes to Fix in Progress. However, when mandatory fields are not yet filled in before transitioning to the next state, my alert/popup appears two times - before the info about the mandatory fields and after I fill them all in. Any suggestions what to change in the script, so that the alert/popup appears only once?
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var alertShown = false;
var RCA_state = 103; // value for Root Cause Analysis state
var FIP_state = 104; // value for Fix in Progress state
if (oldValue == RCA_state && newValue == FIP_state) {
if (!alertShown) {
if (!g_form.mandatoryCheck()) {
alert(getMessage('My alert message'));
alertShown = true;
}
}
} else {
if (newValue != FIP_state && alertShown) {
alertShown = false;
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 07:14 AM
Try adding:
if (isLoading || newValue === '') {
return;
}
if (oldValue == newValue) {
return;
}
at the beginning of your function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 07:14 AM
Try adding:
if (isLoading || newValue === '') {
return;
}
if (oldValue == newValue) {
return;
}
at the beginning of your function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2024 09:21 AM
Hello @kandulek ,
The issue occurs because the onChange Client Script runs every time the State field changes. When mandatory fields aren’t filled, ServiceNow’s validation logic triggers, reverting the state and causing the script to fire again after the fields are completed. Here’s an updated script to ensure the alert only shows once:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '' || oldValue === newValue) {
return; // Exit if loading, empty value, or no change in the state
}
var RCA_state = 103; // Root Cause Analysis state value
var FIP_state = 104; // Fix in Progress state value
if (oldValue == RCA_state && newValue == FIP_state) {
if (!g_form.mandatoryCheck()) {
alert(getMessage('siemens.rca.please.review.and.update.fields'));
// Revert the state to ensure users complete mandatory fields
g_form.setValue('state', oldValue);
}
}
}
What’s Changed:
- Added "oldValue === newValue" check to prevent unnecessary execution when there’s no actual state change.
- Used "g_form.setValue" to revert the state when mandatory fields are incomplete, avoiding repeated alerts.
- Simplified the logic to reduce redundancy, ensuring the alert is shown only once per valid state change.
Why This Works:
The problem occurs because the state change triggers the script multiple times—once when the state initially changes and again after mandatory fields are completed. By reverting the state back if fields aren’t filled, we prevent this behavior while guiding users to complete the required fields.
I tested this in a similar scenario, and it works as expected. Let me know how it works for you!
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Regards,
Aniket