Here’s an example exercise that introduces you to creating and using ServiceNow client
scripts. This exercise is designed for beginners and covers a common use case in ServiceNow
form development.
Exercise: Show a Field Message When a Priority Changes
Scenario
You want to notify the user every time the "Priority" field is changed on an Incident form by
displaying an informational message. This teaches you how to use an onChange client script.
Steps
1. Navigate to Client Scripts
• Go to System Definition → Client Scripts in the ServiceNow application
navigator.
2. Create a New Client Script
• Click New.
• Fill in the fields as follows:
• Name: Show Message on Priority Change
• Table: Incident [incident]
• UI Type: All
• Type: onChange
3. Configure the onChange Script
• Field name: priority
4. Enter the Script
• Copy and paste this JavaScript code into the Script field:
javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
g_form.showFieldMsg('priority', 'You have changed the priority. Please ensure your notes
are updated.', 'info');
}
5. Save the Script
• Click Submit or Update to save and activate the client script.
Test the Script
• Open any Incident record.
• Change the Priority field.
• You should see an informational message appear below the field when the value
changes.
Discussion
• This exercise demonstrates how to use an onChange client script to respond to user
actions in real time.
• You can modify the script to perform different actions, such as making fields
mandatory, showing/hiding fields, or adding validation messages.
Challenge:
Try modifying the script to display a warning message if the priority is set to "1 - Critical" and
only an info message for all other priorities.
Let me know if you’d like more advanced client script exercises or examples!