ServiceNow Blog 02: Types of Client side scripting :

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 06:03 AM - edited 02-06-2024 06:33 AM
Types of Client script:
- On load
- On submit
- On change
- On cell edit
- On load: Script will run when form loads and before control is given the user. Used when something needs to be changed on the form while it is loading. Onload () function will be automatically populated in the script field when onload type is selected on the form.
- Onload client script runs when user opens the form and before user can enter data on the form.it is usually used to set default values on the form or show message or popup while form is opened.
- Major use case of onload client script:
- Showing P1 Message alert if the priority of current incident is P1.
- Setting logged user in the caller field on incident form.
- Showing message at the top of the form if the caller is VIP.
- When P1 incident is opened then there should be an alert which says” this incident is P1 incident”.
- When a new incident form is opened then caller should be populated automatically with current logged in user.
2. On submit: Script will run when form is submitted or saved. Used to perform validations before form is saved. On submit () function will be automatically populated in the script field when on submit type is selected on the form.
- Runs when thin a form is submitted. It is usually used to validate things on the form before the submission of the form.
- Major use case of on submit client script:
- Showing alert before creation of P1 Incident.
- Stop user for creating the incident if assignment group is not correct as per selected category.
- Validate for mandatory fields on different forms.
- When P1 Incident is created then it should ask user to confirm if P1 Incident should be created.
- When Network assignment group is selected, and category is hardware then user should not be able to create the incident.
3. On change: Script will run when value of any field is updated or changed. Used to perform some operation as per the field changed. On change () function will be automatically populated in the script field when on change type is selected on the form.
- Runs when a particular field value changes on the form. It is used when you want to perform some action on client when field value changes.
- Major use case of on change client script:
- Show alert when priority changes to P1.
- Populate assignment group when CI Changes.
- Make assignment group field mandatory when category is hardware.
- When the state of the incident changes to on hold then the work notes field should become mandatory.
- When a category is changed then there should be a message about change of category.
4. On cell edit: Script will run when a field value changes on a list. oncellEdit() function will be automatically populated in the script field when oncellEdit type is selected On the form.
- Script will run when a field value changes on a list.
- The onCellEdit() client script must specify these parameters.
- sysIDs: hold all the value 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.
Note: onCellEdit() client scripts do not apply to dashboard list widgets.
function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
var saveAndClose = true;
//Type appropriate comment here, and begin script below
callback(saveAndClose);
}
- Major use case of oncellEdit client script:
- Prevents users from changing the State to closed in a list.
- 387 Views