IT CanvassTalk to an advisor
ServiceNow Examples · LessonBy , ServiceNow Trainer, 9 yrs · Published · current release · reference

Client script examples

onChange, onLoad and onSubmit client script patterns.

Quick answer

Client Scripts react to form events in the browser. Keep them light and never trust them for security.

Key takeaways
  • onChange: react to a field
  • onLoad: default a field
  • onSubmit: validate

Client Scripts react to form events in the browser. Keep them light and never trust them for security.

onChange: react to a field

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue === '') return;
  g_form.setValue('urgency', newValue);
}

onLoad: default a field

function onLoad() {
  if (g_form.getValue('category') === '')
    g_form.setValue('category', 'inquiry');
}

onSubmit: validate

function onSubmit() {
  if (g_form.getValue('short_description') === '') {
    g_form.addErrorMessage('Description is required');
    return false;
  }
}

Quick check

+0 XPStreak ×0
Question 1 of 2
Where do client scripts run?
Practise this on your own free instance.
Set up your free instance →
Already working on ServiceNow and stuck on a live ticket?Get an expert ServiceNow developer on screen-share to finish your daily tasks with you. Deliver on time, protect your reputation and your job. Monthly support only, no task-wise plans.Task assigned · no idea where to startStill stuck · your job on the lineExpert joins your screenDelivered on timeExplore On Job Support