Client script to check a value on incident form when page is saved/submitted

SC10
Kilo Guru

I am playing around with a new True/False element on my Incident form, that simply asks the end user who is using the Incident form, "Has the location been verified?". I went to True/False, because this is the only way I could find to get a check box on the Incident form.

In brief, I want the end user to Check this True/False, thus making it True, when they have confirmed the customer's location. Because this True/False is a boolean, I have not seen an easy way to make it Mandatory (because what is mandatory in true and false?), so I was wondering if I could use a Client Script that checks if the value of this True/False to be True, and not False, when the page is saved/submitted etc? Any other good way to do this?

Much thanks.

1 ACCEPTED SOLUTION

Daniel Draes
ServiceNow Employee
ServiceNow Employee

Hi Shane,


I have tried similar things and I always switched to a standard string field with Yes/no as a choice list. This way you can have the the default to 'nothing' and thus forcing the user to select either yes or no.



Regards,


Daniel


View solution in original post

17 REPLIES 17

Done that, still not working.



I inspected the checkbox element itself, and this is the HTML behind it. Hopefully it will reveal more?



"



<input value="true" id="ni.incident.u_confirm_location" type="checkbox" data-type="boolean" data-ref="incident.u_confirm_location" name="ni.incident.u_confirm_location">


<input id="incident.u_confirm_location" value="false" gsftlocked="false" type="hidden" name="incident.u_confirm_location">


Last thing I would try is just removing the default value all together.   I checked a few of my Boolean values and none of them have a default value, but whenever I load a new form they are always set to false.



Also, put in some alerts around each line just to make sure you're getting what you want:



var locVerified = g_form.getValue('u_location_verification'); //this is your true/false field name


alert(locVerified);


alert(!locVerified);


  if(!locVerified){ //is our variable set to false?


  alert('Please ensure to verify the location with the customer.');


  return false; //if we return false, then the update/sumbission of the record will be aborted


  }


With that exact code, it does this on submit, when I do not check the Confirm Location checkbox:



1. Sends an alert that says "false".


2. Sends another alert that says "false".


3. Submits the page successfully.


Changed the script, this seems to work now! Also oh god what is happening to this forum, the width on these posts...





function onSubmit(){


      var locVerified = g_form.getValue('u_confirm_location'); //this is your true/false field name


  //   alert(locVerified);


  //   alert(!locVerified);




if(locVerified == 'false') {




              alert('Please ensure to verify the location with the customer.');


      return false; //if we return false, then the update/sumbission of the record will be aborted


}


}


Daniel Draes
ServiceNow Employee
ServiceNow Employee

Hi Shane,


I have tried similar things and I always switched to a standard string field with Yes/no as a choice list. This way you can have the the default to 'nothing' and thus forcing the user to select either yes or no.



Regards,


Daniel