Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

OnChange Client Script Executing after onLoad in workspace

Meet Mewada
Tera Expert

Hi everyone,

 

I’ve created my first OnLoad client script for the Incident table that sets the Assignment Group to a specific value. I also have an OnChange client script tied to the Assignment Group field.

 

In Workspace, I’ve noticed that my OnChange script executes immediately after OnLoad, even though I’ve included the usual check: if (isLoading) return true;

 

From my understanding, this should prevent the OnChange script from firing during the initial load. However, in Workspace it seems to behave differently. FYI, this works very well in native UI.

 

Has anyone else experienced this? Is there a known difference in how Workspace handles OnLoad and OnChange client scripts compared to the classic UI? Any insights or best practices to ensure OnChange doesn’t trigger right after OnLoad would be greatly appreciated.

 

Thanks in advance!

1 ACCEPTED SOLUTION

Hi @Meet Mewada 

Same is happening to me as well, the isLoading not working in the workspace but working as expected in the Native UI.
I added logs in both inside isLoading and outside isLoading, both are triggering on the time of form load. You can raise a Hi ticket for this issue. For workaround - Deactivate your onLoad script and set assignment group through display BR.

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron

@Meet Mewada 

that onChange should not work when form loads if isLoading return true is mentioned

Did you try adding alert within isLoading?

Did you try deactivating it and see?

Did you clear instance cache using cache.do and then logout and login again?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Meet Mewada
Tera Expert

Yes! Indeed i tried to debug using Alerts, cleared Instance Cache but this still persist.

 

 

Hi @Meet Mewada 

Same is happening to me as well, the isLoading not working in the workspace but working as expected in the Native UI.
I added logs in both inside isLoading and outside isLoading, both are triggering on the time of form load. You can raise a Hi ticket for this issue. For workaround - Deactivate your onLoad script and set assignment group through display BR.

AhsanM
Tera Expert

Hi Meet,

 

You have hit a well known difference between classic UI and Workspace. This is not a bug in your script, it is a fundamental architectural difference in how Workspace handles client scripts.

 

Why this happens in Workspace

In classic UI, the isLoading flag is reliably set to true during the initial form load, which correctly prevents onChange from firing when a field value is set programmatically during onLoad. In Workspace however, the form rendering works differently. Workspace uses a component based architecture where field value changes can trigger onChange scripts even during the initial load cycle, and the isLoading flag is not always handled the same way.

 

Workaround 1: Use a flag variable

Set a global flag at the start of onLoad and clear it at the end. Check this flag in your onChange script:

 
// In your onLoad script - add at the very beginning
window.isFormLoading = true;

// your existing onLoad logic here
g_form.setValue('assignment_group', 'YOUR_GROUP_SYS_ID');

// At the very end of onLoad
window.isFormLoading = false;

 

Then in your onChange script:

 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) return;
    if (window.isFormLoading) return; // additional Workspace check

    // your onChange logic here
}

 

Workaround 2: Add a setTimeout in onLoad

Wrap your setValue call in a small timeout so it fires after the initial rendering cycle completes:

 
function onLoad() {
    setTimeout(function() {
        g_form.setValue('assignment_group', 'YOUR_GROUP_SYS_ID');
    }, 100);
}

This gives Workspace enough time to finish the initial load before the value change is applied, preventing onChange from treating it as a user triggered change.

 

Workaround 1 is the most reliable for Workspace specifically.

 

Hope this helps!

Ahsan
ServiceNow Developer & Admin
Builder of NowFixer | Free AI debugging tool for ServiceNow scripts