How to check if any field value changes on the form?

Ipshita Kumari
Kilo Explorer

Hi Team,

Hope you all are doing well.

I have requirement to check if any field values have been modified on the form and make "Suggestion" field mandatory if any of the field value changes.

Can you please suggest how can I achieve this requirement ?

 

Regards,

Ipshita

7 REPLIES 7

rahulpandey
Kilo Sage

Hi Ipshita,

You can use below

g_form.modified;

sachin_namjoshi
Kilo Patron
Kilo Patron

If you just want to do a check to see if any value on the entire form changed (a dirty form), you can use ‘g_form.modified’ in an ‘onSubmit’ client script like this…

 

function onSubmit() {
  if(g_form.modified){
    return confirm("Values on the form have changed.\nDo you really want to save this record?");
  }
}
If you just want to check for changes to a few specific fields then you could use something like this in an ‘onSubmit’ client script…
 
 
function onSubmit() {
  var field1 = g_form.getControl('caller_id'); //Get the 'Caller' field control
  var field2 = g_form.getControl('short_description'); //Get the 'Short description' field control
 
  //See if the 'changed' attribute on either field is true
  if(field1.changed || field2.changed){
    return confirm("Values on the form have changed.\nDo you really want to save this record?");
  }
}

 

Regards,

Sachin

 

 

function onSubmit() {
  var field1 = g_form.getControl('caller_id'); //Get the 'Caller' field control
  var field2 = g_form.getControl('short_description'); //Get the 'Short description' field control
 
  //See if the 'changed' attribute on either field is true
  if(field1.changed || field2.changed){
    return confirm("Values on the form have changed.\nDo you really want to save this record?");
  }
}

This gives an error - onSubmit script error: TypeError: Cannot read properties of undefined (reading 'changed'):
function () { [native code] }

asifnoor
Kilo Patron

Hello Ipshita,

Refer to this article where there is sample code provided for both client and server side.

https://servicenowguru.com/scripting/business-rules-scripting/checking-modified-fields-script/

Mark the comment as a correct answer and also helpful if this  has answered your question.