Client Scripts run in the user's browser and control form behavior. They execute when forms are loaded, when field values change, when forms are submitted, or when list cells are edited.
Client Scripts run on the CLIENT side (browser), not on the server. They cannot directly query the database - use GlideAjax for that.
| Type | When It Runs | Use Case |
|---|---|---|
| onLoad | When form loads | Set default values, make fields mandatory/readonly |
| onChange | When field value changes | Field validation, populate related fields |
| onSubmit | Before form submission | Form validation, prevent submission |
| onCellEdit | When list cell is edited | Validate list inline edits |
Execute when a form is loaded and the DOM is ready.
function onLoad() {
// Make assignment_group mandatory for high priority incidents
if (g_form.getValue('priority') == '1') {
g_form.setMandatory('assignment_group', true);
g_form.showFieldMsg('assignment_group', 'Required for P1 incidents', 'info');
}
// Set default value if empty
if (g_form.getValue('category') == '') {
g_form.setValue('category', 'inquiry');
}
// Hide/Show sections
if (g_form.getValue('state') == '6') { // Resolved
g_form.setSectionDisplay('resolution_info', true);
}
}
Keep onLoad scripts lightweight. Heavy operations can slow down form loading. Avoid database queries in onLoad scripts.
Execute when a specific field value changes. Must specify which field triggers the script.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Don't run during form load or templates
if (isLoading || isTemplate) {
return;
}
// Check if value actually changed
if (oldValue == newValue) {
return;
}
// Get the field name
var fieldName = control.id;
// Business logic based on priority
if (newValue == '1') { // Critical
g_form.setMandatory('assignment_group', true);
g_form.setMandatory('assigned_to', true);
g_form.showFieldMsg('priority', 'Critical incident - immediate assignment required', 'error');
} else {
g_form.setMandatory('assignment_group', false);
g_form.setMandatory('assigned_to', false);
g_form.hideFieldMsg('priority');
}
}
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || isTemplate) {
return;
}
// Clear subcategory when category changes
if (oldValue != newValue) {
g_form.clearValue('subcategory');
// Set filter for subcategory based on category
g_form.addFilter('subcategory', 'category', newValue);
}
}
Execute when form is submitted. Can prevent submission by returning false.
function onSubmit() {
// Validate high priority incidents
var priority = g_form.getValue('priority');
var assignmentGroup = g_form.getValue('assignment_group');
if (priority == '1' && assignmentGroup == '') {
g_form.addErrorMessage('Critical incidents must have an assignment group');
return false; // Prevent submission
}
// Validate resolved incidents have resolution notes
var state = g_form.getValue('state');
var resolutionNotes = g_form.getValue('close_notes');
if (state == '6' && resolutionNotes == '') {
var confirmed = confirm('No resolution notes provided. Continue anyway?');
return confirmed; // Only submit if user confirms
}
return true; // Allow submission
}
Use g_form.addErrorMessage() to show specific error messages. Always return true or false from onSubmit scripts.
Execute when a cell is edited in a list view with inline editing enabled.
function onCellEdit(sysIDs, table, oldValues, newValue, callback) {
// Validate the new value
if (newValue == '' || newValue == null) {
alert('Value cannot be empty');
callback(false); // Reject the edit
return;
}
// Additional validation
if (table == 'incident' && newValue == '1') { // Priority 1
var confirmMsg = 'Setting priority to Critical for ' + sysIDs.length + ' record(s). Continue?';
if (!confirm(confirmMsg)) {
callback(false); // Reject the edit
return;
}
}
callback(true); // Accept the edit
}