Thank you for your feedback.
Form temporarily unavailable. Please try again or contact to submit your comments.

Useful field scripts

Log in to subscribe to topics and get notified when content changes.

Useful field scripts

This is a searchable version of the useful field customization scripts.

Caution: The customization described here was developed for use in specific instances, and is not supported by ServiceNow. This method is provided as-is and should be tested thoroughly before implementation. Post all questions and comments regarding this customization to our community forum.

For an easy-to-navigate version, visit the Useful Scripts portal.

AKA Incident Template, Auto Assignments, Quick Calls, Call Script, Auto Populate

Let's say you want to auto-fill your Short Description based on the selected Subcategory. First, create a lookup table, then populate the key field, in this case the Subcategory and the auto-filled field, Short Description. So let's say your table had a record with Subcategory = Password and Short Description = Password Reset. When the user selects the Subcategory of Password on the Incident form, a client script looks up the matching record and sets Short Description equal to Password Reset.

Client script settings... Type = onChange, Table name = incident, Field name = Subcategory

function onChange(control, oldValue, newValue, isLoading){
    var newrec = gel('sys_row');
    //Check if new record
    if (newrec.value==-1) {
        var lookup =new GlideRecord('u_short_desc_lookup');
        lookup.addQuery('u_subcategory', g_form.getValue('subcategory'));
        var temp; //temp var - reusable
            temp = lookup.u_short_description;
            if(null!= temp) {
                //Set the form value from lookup if there is a lookup value
                g_form.setValue('short_description', temp);
            } else {
          } else {
              //If a lookup record does not exist based on lookup.addQuery
              //Then set to UNDEFINED or NULL depending on type

You could populate multiple fields or even pull Call Script questions into the Comments field so call center personnel gather good information to pass on to a technician. There are already Assignment Rules, Templates, and Wizards built in that perform similar functions.

Disable HTML tags in descriptions

Description: This code disables HTML tags in descriptions and short descriptions by substituting the tags with harmless versions that won't execute.
function doit(){ 
 var desc = current.description.toString();
 var shdesc = current.short_description.toString();
 if(desc.indexOf('script>')>-1|| shdesc.indexOf('script>')>-1){
   desc = desc.replace(/<script>/g,"(script)");
   current.description= desc.replace(/<\/script>/g,"(\/script)");
   shdesc = shdesc.replace(/<script>/g,"(script)");
   current.short_description= shdesc.replace(/<\/script>/g,"(\/script)");}

Eliminate leading and trailing spaces in fields

Table: sys_user

Description: This example of the script trims trailing and leading spaces in the FirstName and LastName fields of sys_user.
function doit(){ 
  var gr =new GlideRecord('sys_user');
    if((gr.first_name.toString().length!= gr.first_name.toString().trim().length)||(gr.last_name.toString().length!= gr.last_name.toString().trim().length)){
     gr.first_name= gr.first_name.toString().trim();
     gr.last_name= gr.last_name.toString().trim();

Make a field label flash

Type: Client script

Description: The following example is for the number field on incident. The label will flash for two seconds:
The arguments for the flash method are as follows:
  1. tablename.fieldname
  2. RGB color or acceptable CSS color like "blue" or "tomato"
  3. integer that determines how long the label will flash:
  • Use 2 for a 1-second flash
  • Use 0 for a 2-second flash
  • Use -2 for a 3-second flash
  • Use -4 for a 4-second flash

Do not specify this argument if you want the field label simply colored the specified color.

Make field label bold

Type: Client script

Description: This script makes the label of a particular field (in this case, Short Description on the Incident Table) bold.
var l = g_form.getLabel('incident.short_description');'bold';}

Make fields read-only

Type: Client script

Table: Incident

Description: This onLoad client script makes fields read-only. For this example, the script makes the following fields on the Incident table read-only: Incident state, Impact, Urgency, Priority, Configuration item, and Assigned to. It also removes the magnifying glass for the read-only Reference Fields (Configuration item and Assigned to).
var incidentState = g_form.getValue('incident_state');
if( incidentState  =='6'|| incidentState  =='7'){

Set current date/time in field

Type: Client script

Description: You can use the following two lines to set the current date and time in a date/time field. This bypasses the problem of getting the value into the proper format and proper timezone.
var ajax =new GlideAjax('MyDateTimeAjax');
    g_form.setValue('put your field name here', ajax.getAnswer());});

System script include

// Be sure the "Client callable" checkbox is checked
var MyDateTimeAjax =Class.create();
MyDateTimeAjax.prototype= Object.extendsObject(AbstractAjaxProcessor,{
    return gs.nowDateTime();}});

Toggle timer field by field name

Type: Client script

Description: Toggles the timer field based on a particular field name.
function toggleTimerByFieldName(fieldName){
 //Step 1: Find the timer object
 //timeObjectName: the timer objects name as it would normally be referenced
 //timeObjectHidden: the hidden input node in the field td
 //timeObjectParent: the parent td node containing the field and it's constituent nodes
 //timeObjectFields: anchor tag with onclick to stop timer
 var timeObjectName = fieldName;
 var timeObjectHidden = gel(timeObjectName);
 //Step 2: simulate click stop button
 var timeObjectParent;
 var timeObjectFields;
 //verify that we got the correct object
    //Get Parent td node
    timeObjectParent = timeObjectHidden.parentNode;
    //Get input fields
    timeObjectFields = timeObjectParent.getElementsByTagName("input");
    //simulate click of stop button
    var timerTestString ="paused";
    var timerImg;
    //loop through input objects looking for the pause timer object
    for(var elIt=0; elIt < timeObjectFields.length; elIt++){
          timerImg = timeObjectParent.getElementsByTagName("img")[0];
          timerImg = timeObjectParent.getElementsByTagName("img")[0];

Modify GlideDateTime field value

Type: A server side script that accesses a GlideDateTime field.

Description: Given a GlideDateTime field or script object, show a variety of ways to easily modify value. The same concept also applies to the GlideDate object.
//You first need a GlideDateTime object
//this can be from instantiating a new object "var gdt = new GlideDateTime()"
//or getting the object from a GlideDateTime field
//getting the field value (for example: var gdt = current.start_date) 
//only returns the string value, not the object
//to get the object use var gdt = current.start_date.getGlideObject();
//now gdt is a GlideDateTime object
var gdt = current.start_date.getGlideObject();
//All methods can use negative values to subtract intervals
//add 1 hour (60 mins * 60 secs)
//add 1 day
//subtract 1 day
//add 3 weeks
//subtract 6 months.
//add 1 year, representing the date and time using the UTC timezone instead of the local user's timezone.