SNOW Development Document 5.
Client scripts Case Studies
1. Validate that the 'short_description' and 'caller' fields are not empty
before permitting form submission.
Script:
function onSubmit() {
var sd = g_form.getValue('short_description'); //To get Short description value
from Incident form
var cal = g_form.getValue('caller_id)'); //To get Caller id from Incident form
if (!sd || !cal) { //Condition is short description and caller id is empty
alert('Short_description and caller name cannot be empty');
return false; //Prevent form from submissioin by aborting the action
}
return true;
}
2. This script requests user confirmation before submitting the form
Script:
function onSubmit() {
if (confirm('Are you sure you want to submit the form')) {
return true; //This will allow form submission
} else {
return false; //Restrict the form from submission
}
}
1 Gandharv Sharma
SNOW Development Document 5.2
3. This script ensures that the end date is not earlier then start date
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Store the value of start date and end date in a variables
var sd = g_form.getValue('u_start_date');
var ed = g_form.getValue('u_end_date');
//Specify the condition to validate the date range
if (ed < sd) {
alert('End date should not be earlier then start date');
return false;
}
return true;
}
4. Applying style to Short description field
Script:
function onLoad() {
//To get control to change style of a field
var sd = g_form.getControl('short_description');
//Set the Background color of the field to yellow
sd.style.backgroundColor = 'yellow';
}
2 Gandharv Sharma
SNOW Development Document 5.2
5. The Target Date Must Be In The Future
Script:
function onSubmit() {
var sd = g_form.getValue('u_end_date'); //Fetch end date value
var cd = new Date();
if (new Date(sd) <= cd) {
alert('Please select the date in the future');
g_form.clearValue('u_end_date');
return false; //Prevent form submission
}
return true; //Allow form submission
}
6. Modify the background color of a field based on priority value
Script:
function onLoad() {
var impact = g_form.getValue('impact');
var urgency = g_form.getValue('urgency');
if (impact === '1' && urgency === '1') {
g_form.getElement('priority').style.backgroundColor = 'red';
} else {
g_form.getElement('priority').style.backgroundColor = 'white';
}
}
3 Gandharv Sharma
SNOW Development Document 5.2
7. Validate a email field using a regular expression
Script
function onSubmit() {
var email = g_form.getValue('u_email');
var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; //Defines a regular
expression (regex) to validate email format
if (!emailRegex.test(email)) { //Tests the email input against the regex
alert('Please enter the email in correct format');
return false; //Prevents the form from being submitted
}
return true; //Allow form submission
}
8. Validate the form before submission and prevent it if short description field
is empty
Script
function onSubmit() {
var sd = g_form.getValue('short_description');
if (!sd) { //check if the short description field is empty
alert('Please enter the short description');
g_form.clearValue('sd');
return false;
}
return true;
}
4 Gandharv Sharma
SNOW Development Document 5.2
9. Replicate the details from caller and number fields
Script
function onSubmit() {
// Check if the form is submitted successfully
if (typeof g_form !== 'undefined' && g_form.submitted && g_form.submitted
=== true) {
// Get the caller's full name and incident number
var callerFullName = g_form.getReference('caller_id');
var incidentNumber = g_form.getValue('number');
var ShortDesc = g_form.getValue('short_description');
// Build the short description
var ShortDescription = ShortDesc + ' - ' + callerFullName.name + ' (' +
incidentNumber + ')';
// Set the short description field with the caller and number
g_form.setValue('short_description', ShortDescription);
}
}
10. Automatically populate the caller's name based on the currently
logged-in user for a new incident records.
Script
function onLoad() {
var caller = g_user.userID;
g_form.setValue('caller_id', caller);
g_form.setDisabled('caller_id', true);
}
5 Gandharv Sharma