top of page

Experiencing challenges with ServiceNow support?

Access professional, reliable assistance tailored to your needs—without breaking your budget.

How to Reload a Form from an onChange Client Script in ServiceNow

Updated: Mar 29

ServiceNow administrators frequently encounter scenarios where forms need dynamic refreshes based on user interactions. One common requirement is reloading a form when a specific field changes. This article provides clarity on implementing this using ServiceNow's client scripts, along with troubleshooting common mistakes.


Understanding the Error and Its Causes

When attempting to reload a ServiceNow form using an onChange client script, users often report that their form does not refresh as expected. Typically, this issue arises due to incorrectly structured JavaScript code or the misuse of certain JavaScript functions.

For instance, consider the following incorrect client script snippet:

if ((oldValue == -5 && newValue == 1) || (oldValue == -5 && newValue == 2)) {
    return confirm('Incident in Pending state can only move to In Progress or Resolved');
    reloadWindow(incident);
    location.reload();
}

The script above contains multiple issues:

  • The return confirm(...) statement immediately terminates the execution, causing subsequent reload commands to never run.

  • The reloadWindow(incident); is not a standard ServiceNow function and will not execute correctly.


How to Properly Reload a Form Using an onChange Client Script

To correctly reload your form in ServiceNow when a particular field value changes, follow these practical steps:

Step 1: Use Correct JavaScript Methods

The simplest and most reliable method to refresh the page or form in an onChange script is using the following:

location.reload();

Alternatively, for specifically reloading within ServiceNow's context, you can use:

g_navigation.reloadWindow();

The g_navigation.reloadWindow() method is particularly beneficial as it works consistently within ServiceNow’s platform and avoids potential browser caching issues.

Step 2: Example Implementation

Here’s a practical example demonstrating how to use g_navigation.reloadWindow() in an onChange client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    if (oldValue == -5 && (newValue != 2 && newValue != 6)) {
        alert('Incident in Pending state can only move to In Progress or Resolved.');
        g_form.setValue('state', oldValue);  // Rollback to the previous valid state
        g_navigation.reloadWindow();         // Reloads the form
    }
}

In this scenario, if the user attempts to change the incident state incorrectly, the script provides feedback and reloads the form, ensuring data integrity.


Alternative Solutions to Form Reloading

Instead of reloading the entire form, consider these efficient alternatives:

  • Preventing Invalid Selections: Remove invalid options proactively using g_form.removeOption(). Example:

if (g_form.getValue('state') == -5) { 
	g_form.removeOption('state', 1);
	g_form.removeOption('state', 4);
}

  • Before Business Rule: Implement a before business rule on the server-side to enforce validation:

if (previous.state == -5 && (current.state != 2 && current.state != 6)) {
	gs.addErrorMessage('Pending incidents can only move to In Progress or Resolved.');
	current.state = previous.state;
}

This method ensures that even backend submissions or API updates adhere to your business logic.


Conclusion

Reloading forms dynamically in ServiceNow requires precise JavaScript implementation. Using correct methods like location.reload() or g_navigation.reloadWindow() in client scripts provides reliable outcomes. However, proactively managing available options or enforcing backend validation via business rules can significantly enhance user experience and platform performance.

Always test these solutions thoroughly in a non-production environment to ensure seamless integration with your existing ServiceNow processes.

Experiencing challenges with ServiceNow support?

Access professional, reliable assistance tailored to your needs—without breaking your budget.

CONTACT

New Zealand HQ

Integrated Knowledge Consulting Office

Level 3, 93 Grafton Road

Auckland

South Korea

Integrated Knowledge Consulting Office

BMY역삼타워 6층

서울특별시 강남구 역삼동 678-10번지

 

info@ikconsulting.com

Thanks for submitting!

  • LinkedIn Social Icon

© Copyright 2025 Integrated Knowledge Consulting. All rights reserved.

bottom of page