This type of client script runs when a form is submitted. It is used to validate things on the form and ensure that the submission makes sense. An onSubmit() client script can cancel form submission by returning a value of false.
-
- Script runs when a form is saved, updated, or submitted
- Typically used for field validation
I have explained the above pic in details in the video. Request you to watch it for better understanding.
Now lets take a look on real word example
Real World Scenario
Requirement: If IT fulfiller clicks on Cancel button then make Cancel Reason field mandatory with alert / error stating cancel reason field is mandatory.
Solution: create onSubmit client script and write below code.
function onSubmit() { if(g_form.getValue('state') ==8 && g_form.getValue('u_cancel_reason') ==''){ g_form.addErrorMessage('Reason field is mandatory !!'); return false; } }
You can not call event from client side, Event will be call on server side only but you can call event in UI action.
Q. Which is executed first UI or client script?
UI Policies execute after Client Scripts. If there is conflicting logic between a Client Script and a UI Policy, the UI Policy logic applies.
Q. How do I stop a form submission in client script Servicenow?
Preventing client-side form submission is very simple. All you need to do is set up an ‘onSubmit’ client script and return ‘false’ in the script.
function onSubmit() { //Type appropriate comment here, and begin script below return false; }