Client Script Example PDF
Client Script Example PDF
This is an extension on the article, Client and Server-side Programming. That article discussed the
differences between client and server-side scripts. In this article, I want to get into more detail with client
scripts. How they work, what they do, and many examples.
For this example, we have five fields, Good, Fast, Cheap, and Result.
Here is an example of a client script for this scenario. Note that I use comments '//' to explain the client
script.
I wrote this just for the change of the Good field. However you probably would also want this for the fast
and cheap fields. That means you would need to also add scripts for those scenarios. This is one of the
drawbacks of client scripts.
When: onChange
onChange of field: u_good
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate)
{
//If the form is loading or the new value of the changing field is
empty exit the script
if (isLoading || newValue == '') {
return;
}
//If Good is true, cheap is true, and fast is true, good luck
if(newValue == 'true' && g_form.getValue('u_cheap') == true &&
g_form.getValue('u_fast') == true) {
//Set the result field on the form.
g_form.setValue('u_result', 'They are dreaming');
}
//repeat for the other scenarios
else if (newValue == 'true' && g_form.getValue('u_cheap') == true
&& g_form.getValue('u_fast') == false) {
g_form.setValue('u_result', 'Will take time to deliver');
}
else if (newValue == 'false' && g_form.getValue('u_cheap') == true
&& g_form.getValue('u_fast') == true) {
g_form.setValue('u_result', 'Not the best quality');
}
else {
g_form.setValue('u_result', 'Who knows');
}
}
I am just going to show the client script part. In this example, a window pops up on form load
function onLoad() {
//Call a UI Page to Display on load.You have to create the UI Page
separately
var gDialog = new GlideDialogWindow('sne_message');
gDialog.setTitle('ServiceNowElite.com Message');
gDialog.setSize(700,700);
gDialog.render();
}
I use this one often. Color code the approval buttons so that they are easier to notice.
It is tempting to use this for many color changes in ServiceNow. How use Field Styles instead as much as
possible.
When: onLoad
Script:
function onLoad() {
var approveButton = document.getElementById('approve');
var rejectButton = document.getElementById('reject');
if (approveButton) {
approveButton.style.background='green';
approveButton.style.color='white';
}
if (rejectButton) {
rejectButton.style.background='red';
rejectButton.style.color='white';
}
}
Before ServiceNow version Dublin was released, I used this to verify a phone number format. Dublin has
new phone number fields.
However it is a good example of regex function. If you are not familiar with regular expressions, here are
some explanations of what regular expressions.
Type: onChange
Field: u_phone_number
Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var tempValue = newValue;
//Use Regular Expressions to verify number
var phoneRegex1 = /^\d{3}-\d{3}-\d{4}$/;
var phoneRegex2 = /^(800|866|877)/;
var phoneRegex3 = /^(1{3}-1{3}-1{4}|2{3}-2{3}-2{4}|3{3}-3{3}-
3{4}|4{3}-4{3}-4{4}|5{3}-5{3}-5{4}|6{3}-6{3}-6{4}|7{3}-7{3}-7{4}|8{3}-
8{3}-8{4}|9{3}-9{3}-9{4}|0{3}-0{3}-0{4})$/;
Alert Example
Type: onChange
Field: u_awesome_check
Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue == 'mike_awesome') {
alert('Yes this is true');
}
}
There is a good example of adjusting slush bucket sizes from the forums.
function onLoad(){
leftBucket.size = height;
rightBucket.size = height;
leftBucket.style.width = width;
rightBucket.style.width = width;
Callback functions make JavaScript far more flexible than it would be otherwise.
Typical functions work by taking arguments as input and returning a result. Functions take an input and
return an output.
Javascript callback functions are different. Instead of waiting for a function to return that result, you can use
a callback to do this asynchronously. This not only helps with performance, it strongly encouraged to use
callback functions and asynchronous programming.
When: onChange
Field: caller_id
function onChange(control, oldValue, newValue, isLoading) {
if (caller.vip == 'true')
alert('Caller is a VIP!');
When: onChange
Field: caller_id
To learn all about client script GlideRecord queries, check out the ServiceNow wiki article.
These are limited glide record queries you can use to make server-side database queries. They use callback
functions to do this. I always suggest using business rules instead for these, but sometimes I can't convince
people to do that.
It is better to use a GlideAjax query than a GlideRecord query. There is a good example in the wiki for
that.
On Change
Table: Configuration Item
Field Name: Product
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate)
{
function setVersion(gr) {
if (gr.next()) {
g_form.setValue('version', gr.u_version);
}
}
This is an easy client script. Remove a value from a choice list if something is set.
When: onChange
Field: Category
Script: