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.

Confirm alert to appear when field value changes

NathanHaywood
Tera Contributor

Hi,

 

I am trying to write an onSubmit Client Script so that when a field value is changed and the form is submitted, a confirm alert is returned which will state 'Expected Closure Date has changed. Approval must be attached once this has been updated.'

 

The script I have so far is below:

 

function onSubmit() {

var u_risk_close_date=g_form.getValue('u_risk_close_date');
if(u_risk_close_date.modified){
return confirm("Expected Closure Date has changed. Approval must be attached once this has been updated.");
}
}

 

Many thanks in advance.

 

1 ACCEPTED SOLUTION

Manmohan K
Tera Sage

Hi @NathanHaywood ,

 

Please try below on submit client script

 

function onSubmit() {
  var risk_close_date = g_form.getControl('u_risk_close_date'); //Get the field control
 
  //See if the 'changed' attribute on field is true
  if(risk_close_date.changed){
    return confirm("Expected Closure Date has changed. Approval must be attached once this has been updated");
  }
}

View solution in original post

4 REPLIES 4

Manmohan K
Tera Sage

Hi @NathanHaywood ,

 

Please try below on submit client script

 

function onSubmit() {
  var risk_close_date = g_form.getControl('u_risk_close_date'); //Get the field control
 
  //See if the 'changed' attribute on field is true
  if(risk_close_date.changed){
    return confirm("Expected Closure Date has changed. Approval must be attached once this has been updated");
  }
}

Hello @Manmohan K 

 

Thank you for your reply.

 

I have made the changes but when going back to the form and amending the u_risk_close_date field, I get this error in red at the top of the page:

 

Error MessageonSubmit script error: TypeError: g_form.Control is not a function:
function () { [native code] }

 

I have copied your script exactly, see below:

 

function onSubmit() {

var u_risk_close_date = g_form.Control('u_risk_close_date'); //Get the field control

//See if the 'changed' attribute of field is true
if(u_risk_close_date.changed){
return confirm("Expected Closure Date has changed. Approval must be attached once this has been updated.");
}
}

 

Thanks again.

 

@NathanHaywood 

 

Correct this line, 

var risk_close_date = g_form.getControl('u_risk_close_date'); //Get the field control

You wrote var u_risk_close_date = g_form.Control('u_risk_close_date'); //missing get before control

Ah, I wasn't paying enough attention clearly. Thank @Manmohan K that's done it!