Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Form screen does not recognize field value changes in client script as record updates.

mugi-san
Tera Guru

Form screen does not recognize field value changes in client script as record updates.

I defined a ClientScript to change the items on the incident screen in accordance

with changes to other items.(onchange script)

Unfortunately, the screen does not seem to detect other field changes

made by the script when you revert the field that was manipulated.

 

Now, if I manually restore the value from what was changed by the client script,

the form screen still recognizes it as "changed" even though it has been restored.

 

This is an unnatural behavior,

so if anyone has any tips or ideas on how to solve this, I would appreciate your help.

2 ACCEPTED SOLUTIONS

Ravi Gaurav
Giga Sage
Giga Sage

Hi @mugi-san 

 

ServiceNow tracks form changes using its internal g_form.modifiedFields array.
When you change a field via a script (like g_form.setValue()), it updates the UI but doesn’t always sync that change into the modified state tracking - especially if the same field is later changed by the user to the same value.

This leads to the form being in a “dirty” state incorrectly.

-------------------------------------

After your scripted updates, you can reset the modified state:

g_form.setValue('category', 'hardware'); g_form.modifiedFields = []; // resets form change tracking

Use this carefully - it resets tracking for all fields.

If you only want to reset one field:

 

 
g_form.setValue('category', 'hardware'); g_form.modifiedFields = g_form.modifiedFields.filter(function(field) { return field !== 'category'; });
--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

mugi-san
Tera Guru

Hi, Everyone.


I have confirmed that the script below works on the standard UI16 interface but does not work in Workspace.
The fact that it does not work in Workspace is by design, as indicated KB0855260 .

Furthermore, I was unable to find any method or property in Workspace that allows detecting whether changes have occurred.

However, as shown Detecting Form changes in CSM/Agent Workspace - ServiceNow Community,
it seems possible to detect updates.

If anyone knows of a logic that can detect changes in Workspace,

I would greatly appreciate it if you could continue investigating.

At the very least, if you're using the standard UI16 interface,

the implementation below should be helpful.

    var targetKey = "problem.u_custom_choice";
    alert("Before:" + JSON.stringify(g_form.modifiedFields));

    Object.keys(g_form.modifiedFields).forEach(function(key) {
        if (key == targetKey) {
            delete g_form.modifiedFields[key];
        }
    });
    alert("Clean:" + JSON.stringify(g_form.modifiedFields));

    g_form.modifiedFields['problem.u_custom_choice'] = true;
    alert("After:" + JSON.stringify(g_form.modifiedFields));

Regards. 

View solution in original post

7 REPLIES 7

Hi @mugi-san ,

 

It seems you didn't implement g_form.modifiedFields correctly as below post by Ravi mentioned 

g_form.setValue('category', 'hardware'); g_form.modifiedFields = g_form.modifiedFields.filter(function(field) { return field !== 'category'; });

Try it out.

 

Did you got a chance to go through the reference mentioned in my previous post.

 

Thanks,
Bhimashankar H

 

-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!

Hi, @Bhimashankar H .

This script doesn't work because `.filter` is not a valid function.

 

Based on the hint I received from @Ravi Gaurav , 

I confirmed that the items are controlled via a JSON object.

However, modifying this JSON object only works on standard UI16 screens

and does not function in Next Experience environments such as Workspace,

so it was not a viable solution. (It is effective in some cases.)

var str = '{"problem.u_custom_choice":true}';
g_form.modifiedFields = JSON.parse(str);



mugi-san
Tera Guru

Hi, Everyone.


I have confirmed that the script below works on the standard UI16 interface but does not work in Workspace.
The fact that it does not work in Workspace is by design, as indicated KB0855260 .

Furthermore, I was unable to find any method or property in Workspace that allows detecting whether changes have occurred.

However, as shown Detecting Form changes in CSM/Agent Workspace - ServiceNow Community,
it seems possible to detect updates.

If anyone knows of a logic that can detect changes in Workspace,

I would greatly appreciate it if you could continue investigating.

At the very least, if you're using the standard UI16 interface,

the implementation below should be helpful.

    var targetKey = "problem.u_custom_choice";
    alert("Before:" + JSON.stringify(g_form.modifiedFields));

    Object.keys(g_form.modifiedFields).forEach(function(key) {
        if (key == targetKey) {
            delete g_form.modifiedFields[key];
        }
    });
    alert("Clean:" + JSON.stringify(g_form.modifiedFields));

    g_form.modifiedFields['problem.u_custom_choice'] = true;
    alert("After:" + JSON.stringify(g_form.modifiedFields));

Regards.