- ClientScript:
If you want to change the form for look and feel like for ex .keep a particular field mandatory or if you want to perform form validation or field validation; for these kind of things there is a need to use clientscript in servicenow. Clientscript is always run on browser. There are two types of script.
- Client side Script: In Client side script, Script is writing for front-end i.e. User Interface. Client side script runs on the browser (Client Machine). Users who know JavaScript can define scripts to run in the client browser. Client scripting uses either data available on the client or data retrieved from the server. Several types of scripts are supported in clientScript.
- Server side Script: In Server side script, Script is writing for Back-end operations i.e. Database operations like insertion, Updation, Deletion etc. Server side script executes at server side machine.
- Script Types in ClientScript:
- onLoad(): This Script type used when The system loads a form. An onLoad() script runs when a form is first drawn and before control is given to the user to begin typing. Typically, you use an onLoad() script to perform some client side manipulation of the document on screen. An onLoad() script must contain a function named onLoad().
- onChange(): This Script type used when the system loads a form and no field values have changed or A field value changes. An onChange() script can run when a particular field changes value or when the form is first drawn and no fields have changed value from previous script actions. As such, onChange scripts can potentially act as onLoad scripts with extra conditions. An onChange() script must contain a function named onChange().
All onChange() scripts are called with several parameters:
- control – the field whose value changed if any.
- oldValue – the value of this widget when the record was loaded into the form.
- newValue – the value of this widget after the change, or when the isLoading parameter is true, the same value as the oldValue parameter.
- isLoading – identifies whether the form is loading and also indicates that no fields have changed value. This parameter can only be true when the form is loading and no values have changed.
- isTemplate – identifies whether the change occurs as part of a template load or not.
- onSubmit():This Script type used when a user saves the form. An onSubmit() script runs when a form is submitted. Typically, you use an onSubmit() script to validate things on the form and ensure that the submission makes sense. As such, onSubmit() scripts can potentially cancel a submission by returning false. An onSubmit() script must contain a function named onSubmit().
- onCellEdit():This Script type used when a list cell value changes. With the exception of onCellEdit client scripts, UI policies and client scripts apply to forms only. Scripts can be defined as onCellEdit to run on the client side when the list editor interacts with a cell.
An onCellEdit() script must contain a function named onCellEdit().
An onCellEdit() script takes the following parameters:
- sysIDs – An array of the sys_ids for all items being edited.
- table – The table of the items being edited.
- oldValues – The old values of the cells being edited.
- newValue – The new value for the cells being edited.
- callback – A callback that continues the execution of any other related cell edit scripts. If ‘true’ is passed as a parameter, the other scripts are executed or the change is committed if there are no more scripts. If ‘false’ is passed as a parameter, any further scripts are not executed and the change is not committed.
First: OnLoad
Second: OnLoad
Third: OnLoad
Forth: OnLoad
Fifth: On Change
Sixth: On Cell Edit