Please use docs.servicenow.com for the latest documentation.

This site is for reference purposes only and may not be accurate for the latest ServiceNow version

Useful Task Scripts

From Wiki Archive
Jump to: navigation, search

This is a searchable version of the Useful Task Scripts. For an easy-to-navigate version, visit the Useful Scripts portal.


Allow Self-Service Users to View Other Incidents


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Auto-Assign to User on Accept

Type: Client Script

Table: Incident

Description: This is an onChange Client Script that will set the Assigned To to the person who is changing the incident state to Active.

Parameters: Field Name: Incident State

Script: <source lang="javascript">function onChange(control, oldValue, newValue, isLoading) {

  //If the page isn't loading
  if (!isLoading) {
     //If the new value isn't blank
     if(newValue != ) {
        //Type appropriate comment here, and begin script below
           if (newValue == '2'){
              var isItil = g_user.hasRole('itil');
                 if (isItil){
                    g_form.setValue('assigned_to', g_user.userID);
                 }  
           }
     }
  }

} </source>


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

name = Change Urgency to High if User is VIP
type = Client Script
table = Incident
param = Field = Caller
description = This is an onChange client script that will check to see if the caller is a VIP (based on the VIP field on the user record), if so change the urgency to high.

<source lang="javascript">function onChange(control, oldValue, newValue, isLoading) {

  //If the page isn't loading
  if (!isLoading) {
     //If the new value isn't blank
     if (newValue != ) {
        //If inc is already urgency of high no need to go on
        if (g_form.getValue('urgency') != 1){ 
           var caller = g_form.getReference('caller_id');
             if (caller.vip=='true') {
                g_form.setValue('urgency', '1');
             }      
        }
     }
  }

} </source>

If you are already using the default client script: Highlight VIP Caller, you can add in the following line to that client script: <source lang="javascript">g_form.setValue('urgency', '1');</source> after this line: <source lang="javascript">if(caller.vip=='true') {</source>


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Close and Cascade Comments to Child Tasks

Type: Business Rule

Table: Incident

Description: This example will close and cascade comments to child incidents after the parent has been closed.

Parameters: Run: After insert and update

Script:

Condition: <source lang="javascript">current.active.changesTo(false)</source> Script: <source lang="javascript">cascadeComment();

function cascadeComment(){

var cmt = current.comments;
var inc = new GlideRecord("incident");
inc.addQuery("parent", "=", current.sys_id);
inc.query();
  while (inc.next()) {
   inc.comments = cmt;
   inc.incident_state.setValue( 7);
   inc.active.setValue(false);
   inc.update();
   gs.print("Incident " + inc.number + " closed based on closure of incident " + current.number);
  }

}</source>


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Close Old Tasks

Type: Scheduled Script

Table: incident

Description: This script checks daily and closes any incident over 30 days old.

Parameters:

Script: <source lang="javascript"> var oldIncident = new GlideRecord('incident'); oldIncident.addQuery('opened_at', '<', gs.daysAgoStart(30)); oldIncident.addQuery('incident_state', '!=', '7'); oldIncident.query();

while (oldIncident.next()) {

 oldIncident.incident_state = 7;
 oldIncident.update();

}</source>

As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Display Task Number as Window Title

Type: UI Script

Table:

Description: This UI Script populates the browser window with the number and short description of the incident being viewed. UI Script should be marked as global.

Parameters:

Script: <source lang="javascript">function setTitle(){

 try {
   var num = g_form.getValue("number");
   var sd = g_form.getValue("short_description");
 } catch(e) {
   // alert('Error getting title info.');
 }
 if (num && sd) {
   top.document.title = num + ' - ' + sd;
 } else {
   top.document.title='ServiceNow - IT Service Management Suite';
 }

}

addLoadEvent(setTitle);</source>


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Log Group Activity

Type:

Table:

Description: This script logs all changes made to an incident by a group. In order to use the script, first create a new table (Group Activity) then create a couple reference fields referencing the data you are looking to capture (in this example, Incident and Assignment Group).

Parameters:

Script: <source lang="javascript">//Create an log file of the group assignment

  var ex = new GlideRecord('group_activity');
  ex.initialize();
  ex.u_assignment_group = current.assignment_group;
  ex.u_incident = current.sys_id;
  ex.insert();

</source>


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Duplicate a Record Along with Related List

Type: UI Action

Table:

Description:

Parameters:

Script: <source lang="javascript"> //clear the fields we dont want to copy current.start_date = ; current.end_date = ;

//set some new default values current.u_change_phase = 'Draft'; current.approval = 'not requested'; current.state = 1; //open state

//insert a copy and copy affected CIs var oldid = current.sys_id.toString(); var newid = current.insert();

if (newid) {

 var taskcis = new GlideRecord('task_ci');
 taskcis.addQuery('task',oldid);
 taskcis.query();
 while (taskcis.next()) {
   taskcis.task = newid;
   taskcis.insert();
 }

}

//copy affected companies if (newid) {

 var crComp = new GlideRecord('u_m2m_companies_change_request');
 crComp.addQuery('u_change_request',oldid);
 crComp.query();
 while (crComp.next()) {
   crComp.u_change_request = newid;
   crComp.insert();
 }

}

//bring the user to the new record action.setRedirectURL(current); </source>


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Find Computer Assigned to Caller and Populate CI Field

Type: Client Script

Table: incident

Description: This script checks what computer is assigned to the caller, and fills in the information in the affected CI field if the CI field is blank.

Parameters: OnChange of Caller ID

Script: <source lang="javascript"> function onChange(control, oldValue, newValue, isLoading) {

  //If the page isn't loading
  if (!isLoading) {
     //If the new value isn't blank
     if(newValue != ) {
        //check to see if we have a CI listed 
        var ci = g_form.getValue('cmdb_ci');
         if(ci == ){

var user = g_form.getValue('caller_id'); //get the current user

   	    var gr = new GlideRecord('cmdb_ci_computer'); //Table your computers reside in
           gr.addQuery('assigned_to', user);
           gr.query();
           if(gr.next()){
              g_form.setValue('cmdb_ci', gr.sys_id);
           }
        }
     }
  }

} </source> This script is the same, except it checks other tables if it does not find the computer:

Script: <source lang="javascript"> function onChange(control, oldValue, newValue, isLoading) {

   //If the page isn't loading
   if (!isLoading) {
      //If the new value isn't blank
      if(newValue != ) {
        //check to see if we have a CI listed
        var ci = g_form.getValue('cmdb_ci');
        if(ci == ){
          var user = g_form.getValue('caller_id');
          var gr = new GlideRecord('cmdb_rel_person'); //Table your relationship resides in
          gr.addQuery('user', user);
          gr.query();
          if(gr.next()){
             g_form.setValue('cmdb_ci', gr.ci);
          }else{
             var gr1 = new GlideRecord('cmdb_ci_computer'); //Table your computers reside in
             gr1.addQuery('assigned_to', user); //or used by or whatever field you are using
             gr1.query();
             if(gr1.next()){
               g_form.setValue('cmdb_ci', gr1.sys_id); 
             }
          }
        }
      }
    }

}</source>


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Hide Activity Formatter on Form based on Role

Type: Client Script

Table: Task

Description: This removes the activity section on task forms for anyone who doesn't have the admin role.

Parameters:

Script: <source lang="javascript">function onLoad() {

  //hide the activity section for users that do not have the admin role
 var isAdmin = g_user.hasRole('admin'); //Check if user has admin role
 if(!isAdmin){
   //activity section does not have an id
   //get all of the TD tag elements in the document
   var refs = document.getElementsByTagName("TD");
   if (refs) {
     for (var i=0; i < refs.length; i++) {
       var ref = refs[i];
       var inner = ref.innerHTML;
       //check for the ID of the activity section label
       if (inner.indexOf("formatter.incident_activity.xml") > 0){
         ref.style.display = 'none';
       }
     }
   }
 }

} </source>

As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.


Name: Indicating Updated Tickets

Type: Field Styles

Table: incident

Description: The first field style creates a small indicator next to incidents that have been updated since the user's last view of the list. The second field style simply indents the other incident numbers so that they still line up.

Note
Note: If this list is viewed in Internet Explorer as a related list on a tabbed form, Internet Explorer will display a warning to the user about viewing unsecured content.

Parameters:

Script:

First Field Style

Condition Value:

javascript:(current.assigned_to.user_name==gs.getUserName()&&current.assigned_to.user_name !=current.sys_updated_by)&&current.incident_state < 7

Style:

background-image: url('images/icons/bullet.gif');
background-repeat: no-repeat;
background-position:2 2;
text-indent: 15px;


Second Field Style

Condition Value:

javascript:!(current.assigned_to.user_name==gs.getUserName()&&current.assigned_to.user_name !=current.sys_updated_by)||current.incident_state == 7

Style:

text-indent: 15px;


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Make Close Note Mandatory

Type:

Table: incident

Description: This script makes the Close Notes field on a form mandatory when clicking the submit button to close.

Parameters:

Script: <source lang="javascript">function onSubmit() {

 var action = g_form.getActionName();     
 var inc_state = g_form.getValue('incident_state');
 if (action == 'close_incident' || inc_state == '6' || inc_state == '7') { 
   var close_note = g_form.getValue('close_notes');                  
   if (close_note == ) {
     alert('Close Notes are mandatory!');             
     g_form.setMandatory('close_notes', true);             
     return false;           
   }          
 }         

}</source>


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Make Closed Tasks Read-Only

Type: Client Script

Table: incident

Description: This script makes closed incidents read-only to Itil users, while allowing admin users to continue to edit them. Note that because this script runs client-side, it is dependent on having the 'incident_state' field on the form (although this field could be hidden by a client script or UI policy).

Parameters:

Script: <source lang="javascript"> function onLoad() {

  var vincident_state = g_form.getValue('incident_state');
  var isItil = g_user.hasRole('itil');
  var isAdmin = g_user.hasRole('admin');
  var incidentClosed = false;
  if (vincident_state == 7 || vincident_state == 5) {
     incidentClosed = true;
  }
  var dArray = new Array();
  var di = 0;
  var email_icon = true;
  var attachment_icon = true;
  var lookup_icon = true;
  var infoBox_icon = true;
  var calendar_icon = true;
  if (incidentClosed) {
     email_icon = false;
     attachment_icon = false;
     lookup_icon = false;
     infoBox_icon = false;
     calendar_icon = false;
     email_icon = false;
  }
  if (!isAdmin && (!email_icon || !attachment_icon || !lookup_icon || !infoBox_icon || !calendar_icon)) {
     var refs = document.getElementsByTagName("a");
     if (refs) {
        for (var i=0; i < refs.length; i++) {
           var ref = refs[i];
           var inner = ref.innerHTML;
           if (!attachment_icon && inner.indexOf("attachment.gifx") > 0) {
              ref.style.display = 'none';
           }
           if (!email_icon && inner.indexOf("email.gifx") > 0) {
              ref.style.display = 'none';
           }
           if (!lookup_icon && inner.indexOf("reference_list.gifx") > 0) {
              ref.style.display = 'none';
           }
           if (!infoBox_icon && inner.indexOf("log.gifx") > 0) {
              ref.style.display = 'none';
           }
           if (!calendar_icon && inner.indexOf("small_calendar.gifx") > 0) {
              ref.style.display = 'none';
           }
        }
        var form = document.getElementById('incident.do');
        if (form) {
           for (var x=0; x< form.elements.length; x++) {
              form.elements[x].disabled=true;
           }
           alert('This incident is closed and can only be updated by an administrator');
        }
     }
  }

} </source>


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Make Field Mandatory for Resolved Tasks

Type: Client Script

Table: incident

Description: These script makes a field mandatory once an incident is closed or resolved. The first script makes the field (u_mandatory) mandatory once the status changes. The second script makes the field (u_resolution) mandatory once the submit button is pressed.

Parameters:

Script:

Option 1: <source lang="javascript"> function onChange(control, oldValue, newValue, isLoading) {

 if (newValue == '6' || newValue == '7')
    g_form.setMandatory('u_symptom', true);
 else
    g_form.setMandatory('u_symptom', false);
} 

</source> Option 2: <source lang="javascript"> function onSubmit(){

 var incident_state = g_form.getValue('incident_state');
 if (sClsBtn.value == 'close_incident' || incident_state == '6' || incident_state == '7') {
     var resolution=g_form.getValue('u_resolution');  
     if (resolution == ) {
         alert('Resolution is mandatory');
         var sa_ctrl=g_form.getControl('u_resolution');
         return false;
     }
 }

} </source>


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.
Criteria Value
name Make Time Worked Related List Mandatory On Close
type Client Scripts
table incident
param  
description In order to use a related list of time worked (allowing multiple users to log different time worked values), two client scripts need to be attached. First, an onLoad client script to stop the timer from going forward automatically, and secondly an onSubmit client script to check whehter a value has been added to the timer.

onLoad

<source lang="javascript"> function onLoad() { var timer_field = document.getElementById('incident.time_worked'); var timer_value = timer_field.value; if (timer_value != "00:00:00") { toggleTimerByFieldName('incident.time_worked'); } } function toggleTimerByFieldName(fieldName) { //Step 1: Find the timer object

//timeObjectName: the timer objects name as it would normally be reference //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 on stop button var timeObjectParent; var timeObjectFields;

//verify that we got the correct object if (timeObjectHidden.type == "hidden") {

//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++) { if (timeObjectFields[elIt].id.match(timerTestString)) { if(timeObjectFields[elIt].value == "false") { timeObjectFields[elIt].value = "true"; timerImg = timeObjectParent.getElementsByTagName("img")[0]; timerImg.src = "images/timer_start.gifx"; } else if (timeObjectFields[elIt].value == "true") { timeObjectFields[elIt].value = "false"; timerImg = timeObjectParent.getElementsByTagName("img")[0]; timerImg.src = "images/timer_stop.gifx"; } } } } } </source>

onSubmit Script

<source lang="javascript"> function onSubmit() { var tw = checkTimeWorked('incident.time_worked'); if (!tw) { var msg = "The Time Worked field is Required. Please input the time worked on this incident."; g_form.showErrorBox("time_worked", msg); return false; } return true; } function checkTimeWorked(fieldName) { var timeObjectName = fieldName; var timeObjectHidden = gel(timeObjectName);

var timeObjectParent; var timeObjectFields;

if (timeObjectHidden.type == "hidden") { timeObjectParent = timeObjectHidden.parentNode; timeObjectFields = timeObjectParent.getElementsByTagName("input"); var timerTestString = "paused";

//loop through input objects looking for the pause timer object for(var elIt=0;elIt<timeObjectFields.length;elIt++) { if (timeObjectFields[elIt].id.match(timerTestString)) { var tof = timeObjectFields[elIt].id; var tmr_base = tof.substring(0, tof.length-(timerTestString.length+1)); var tmr_hour = tmr_base + "_hour"; var tmr_min = tmr_base + "_min"; var tmr_sec = tmr_base + "_sec"; var hour = g_form.getValue(tmr_hour); var min = g_form.getValue(tmr_min); var sec = g_form.getValue(tmr_sec); var two = hour + ":" + min + ":" + sec; var twn = g_form.getValue('time_worked'); if (two == twn) return true; if (hour == '00' && min == '00') { return false; } } } } return true; } </source>


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Prevent Closure if Child Task is Active

Type: "Before Update" Business Rule

Table:

Description: Prevents closing a task if any of the task's child tasks are still active.

Parameters:

Script: <source lang="javascript">var gr = new GlideRecord('task'); gr.addQuery('active','true'); gr.addQuery('parent',current.sys_id); gr.query(); if (gr.next()) { current.setAbortAction(true); }</source>


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Prevent Priority One Ticket Submission If Impact Isn't High

Type: Client Script

Table:

Description:

Parameters:

Script: <source lang="javascript">function onSubmit() {

     var priority = gel('incident.priority');   
var impact = gel('incident.impact');
if (!priority)
    return true;
if (!impact)
    return true;
if (priority.value == 1 && impact.value > 2) {
    alert('Cannot open a priority 1 incident on a low impact event');
    return false;
}

} </source>

Obsolete
Functionality described here is obsolete {{ #if: Spring 2009 | since the Spring 2009 release}} {{ #if: Global Text Search | and has been replaced by Global Text Search}}.
As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.


Script Name: Search Tasks with kb find

Type: UI Page

Description: The following script is a modification of the out-of-the-box "kb_find" UI page. It modifies the existing Knowledge Base search functionality by returning results from the Incident, Problem, and Change Request tables as well as the KB results. This script predates the Text Search Plugin, which is a more global search and may be more suited to the situation.

The script is set up to return no more than 20 results from the task tables so as to avoid returning potentially thousands of results. The maximum number of results returned from the task tables can be changed by modifying the "<j2:while test="$[ctr != 20]">" line below.

You can implement this script simply by replacing the code in the out-of-the-box "kb_find" UI page with the code below. The "kb_find" page can be found by navigating to the "System UI" application and selecting the "UI Pages" module.

Script

<?xml version="1.0" encoding="utf-8" ?>
<!--
 Knowledge management v2 - list viewer 
-->
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
 <link href="kb_styles.cssx" type="text/css" rel="stylesheet" />
 <SCRIPT LANGUAGE="JavaScript" SRC="kb.jsx?v=${gs.getProperty('glide.builddate')}" />
 
 <g2:evaluate jelly="true" var="jvar_item">
 var kb = new GlideRecord('kb_knowledge');
 var task = new GlideRecord('task');
 var qc = task.addQuery('sys_class_name', 'problem');
 qc.addOrCondition('sys_class_name', 'incident');
 qc.addOrCondition('sys_class_name', 'change_request');
 <!--task.orderByAsc('number');-->
 
 
 var operator = 'IR_AND_QUERY';
 if (jelly.sysparm_operator) 
 operator = jelly.sysparm_operator;
 
 if (jelly.sysparm_search $[AND] jelly.sysparm_search.length != 0)
 kb.addQuery(operator, jelly.sysparm_search);
 task.addQuery(operator, jelly.sysparm_search);
 
 if (jelly.sysparm_topic $[AND] jelly.sysparm_topic.length != 0 $[AND] jelly.sysparm_topic != 'null') {
 gs.print("adding topic = " + jelly.sysparm_topic);
 kb.addQuery('topic', jelly.sysparm_topic);
 }
 
 if (jelly.sysparm_category $[AND] jelly.sysparm_category.length != 0 $[AND] jelly.sysparm_topic != 'null') {
 gs.print("adding category = " + jelly.sysparm_category);
 kb.addQuery('category', jelly.sysparm_category);
 }
 
 kb.addQuery('active', 'true');
 kb.addQuery('workflow_state', 'published');
 var orderField = 'relevancy';
 var sortSequence = gs.getProperty('glide.knowman.order.search');
 if (sortSequence != 'relevancy') {
   kb.orderByDesc('sys_view_count');
   orderField = 'view count';
 }
 kb.query();
 
 <j2:if test="$[gs.hasRole('itil')]">
 task.query();
 </j2:if>
 
 </g2:evaluate>
 
 <table class="wide" cellspacing="0" border="0" style="margin-bottom: 8px;">
 <tr>
 <td class="title" nowrap="true" colspan="99">
 <g:inline template="kb_header.xml" />
 </td>
 </tr>
 </table>
 <table class="wide" cellspacing="0" border="0" style="margin-bottom: 8px;">
 <tr class="header" border="0" cellspacing="0">
 <td class="column_head" colspan="3">
 <table border="0" cellspacing="0" cellpadding="0">
 <tr class="header" border="0" cellspacing="0">
 <form method="GET" action="${sysparm_base_form}.do" name="${sysparm_base_form}.do">
 <input type="HIDDEN" name="sys_action" value="none" />
 <g2:emitParms suppress="sysparm_this_url_enc" />
 <input type="HIDDEN" name="sysparm_modify_check" value="true" />
 <td>
 <input type="HIDDEN" id="sysverb_back" />
 <img name="not_important" 
 value="sysverb_back" 
 id="sysverb_back" 
 onClick="return gsftSubmit(document.getElementById('sysverb_back'));" 
 src="images/green_back.gifx" 
 title="${gs.getMessage('Back')}" 
 alt="${gs.getMessage('Back')}" 
 style="cursor:hand; margin-left: 4px;"/>
 </td>
 </form>
 <td>
 <div class="caption" style="margin-top: 2px;">
   $[gs.getMessage('kb.banner.title')] 
   $[gs.getMessage('search results')]
   ($[gs.getMessage('sorted by')] $[gs.getMessage(orderField)])
 </div>
 </td>
 </tr>
 </table>
 </td>
 </tr>
 <j2:set var="jvar_printed_some" value="false"/>
 <j2:set var="jvar_printed_some_task" value="false"/> 
	<g2:inline template="kb_search_results.xml"/>
	<j2:if test="$[jvar_printed_some == false]">
		<g2:evaluate var="jvar_item" expression="gs.flushMessages();"/>
		<tr>
		<td>
		   <div class="kb_no_text">Your search - <span class="kb_stand_out">$[sysparm_search]</span> - did not match any documents.</div>
		</td>
		</tr>
	</j2:if> 
 
<tr border="0" cellspacing="0"><td><br /></td></tr> 
 
<tr class="header" border="0" cellspacing="0">
<td class="column_head" colspan="3">
<table border="0" cellspacing="0" cellpadding="0">
 <tr class="header" border="0" cellspacing="0">
 <form method="GET" action="${sysparm_base_form}.do" name="${sysparm_base_form}.do">
 <input type="HIDDEN" name="sys_submitted" value="false" />
 <input type="HIDDEN" name="sys_action" value="none" />
 <g2:emitParms suppress="sysparm_this_url_enc" />
 <input type="HIDDEN" name="sysparm_modify_check" value="true" />
 <td>
 <input type="HIDDEN" id="sysverb_back" />
 <img name="not_important" 
 value="sysverb_back" 
 id="sysverb_back" 
 onClick="return gsftSubmit(document.getElementById('sysverb_back'));" 
 src="images/green_back.gifx" 
 title="${gs.getMessage('Back')}" 
 alt="${gs.getMessage('Back')}" 
 style="cursor:hand; margin-left: 4px;"/>
 </td>
 </form>
 <td>
 <div class="caption" style="margin-top: 2px;">
 Task results
 </div>
 </td>
 </tr>
 </table>
</td>
</tr>
 
<g2:evaluate>
 var ctr = 0;
</g2:evaluate>
 
 <j2:set var="jvar_print_section" value="false"/>
 <j2:while test="$[ctr != 20]">
 <g2:evaluate>
 <j2:if test="$[task.next()]">
 <j2:set var="jvar_print_section" value="true"/>
 </j2:if>
 </g2:evaluate> 
 <j2:if test="$[jvar_print_section == true]">
 <j2:set var="jvar_printed_some_task" value="true"/>
 <j2:set var="jvar_task_link" value="task.do?sys_id=$[task.sys_id]"/>
 <tr>
 <td>
 <div style="margin-top: 10px;"></div>
 <g2:evaluate var="jvar_desc">
 var answer = task.short_description;
 answer;
 </g2:evaluate>
 <div><a href="$[jvar_task_link]" class="kb_search_header"><g2:highlighter search="$[sysparm_search]">$[jvar_desc]</g2:highlighter></a></div>
 <div><g2:highlighter search="$[sysparm_search]">$[task.description]</g2:highlighter></div>
 <div class="kb_add_info">
 Result type:$[SP]<font style="text-transform: uppercase;">$[task.sys_class_name]</font>$[SP]>
 <j2:set var="jvar_got_stackname" value="$[sysparm_nameofstack.length() > 0]"/>
 <j2:if test="$[jvar_got_stackname == false]">
 <a class="kb_add_info_link" href="task.do?sys_id=$[task.sys_id]">$[task.number]</a>
 </j2:if>
 </div>
 </td>
 </tr>
 </j2:if>
 <g2:evaluate> 
 ctr++;
 </g2:evaluate>
 <j2:set var="jvar_print_section" value="false"/> 
 </j2:while>
 
 <j2:if test="$[jvar_printed_some_task == false]">
 <g2:evaluate var="jvar_item" expression="gs.flushMessages();"/>
 <tr>
 <td>
 <div class="kb_no_text">Your search - <span class="kb_stand_out">$[sysparm_search]</span> - did not match any Tasks.</div>
 </td>
 </tr>
 </j2:if>
 
 </table>
</j:jelly>


As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Warn User Before Submitting a Priority One Ticket

Type: onSubmit Client Script

Table: Incident

Description: An onSubmit Client Script throws an alert if a ticket is inserted with Priority = 1.

Parameters:

Script: <source lang="javascript"> // Only alert if ticket is being inserted (as opposed to modified) function onSubmit() {

   if (g_form.getValue('priority') == '1') {
       return confirm('Submit a priority one ticket?');
   }

} </source>

As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Update Incident with Attachments

Type: Inbound Email Action

Table:

Description: The following Inbound Email Action when used in an "Update" or "Replied" rule, will copy the attachment to the current table that is being updated.

Parameters:

Script:

Note
Note: This API call changed in the Calgary release:
  • GlideSysAttachment replaces Packages.com.glide.ui.SysAttachment

The new script object calls apply to the Calgary release and beyond. For releases prior to Calgary, substitute the packages call as described above. Packages calls are not valid beginning with the Calgary release. For more information, see Scripting API Changes.

<source lang="javascript">// query for the sys_email record (incoming email) that contains the attachment var email_log = new GlideRecord('sys_email'); email_log.query('uid', email.uid); email_log.orderByDesc('sys_created_on'); email_log.query(); if(email_log.next()) {

 // Add the attachment to task
 GlideSysAttachment.copy("sys_email", email_log.sys_id, current.sys_class_name, current.sys_id);

} </source>

As-is functionality
Caution: The customization described here was developed for use in specific ServiceNow instances, and is not supported by ServiceNow Customer Support. 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.

Name: Validate Change Windows

Type:

Table:

Description: This uses both a client script and a global business rule (called via AJAX) to validate a change window against a schedule attached to the configuration item.

Parameters:

Script: Client script <source lang="javascript"> function onChange(control, oldValue, newValue, isLoading) {

 if (isLoading){
     return;
  }

  var startDate = g_form.getValue('start_date');
  var endDate = g_form.getValue('end_date');
  var scheduleID = g_form.getValue('u_maint_window');

  if(startDate !=  && endDate !=  && scheduleID != ){
     g_form.hideErrorBox("u_maint_window");
     var ajax = new GlideAjax('MySysScriptInclude');
     ajax.addParam('sysparm_name','globalCheckWindows');
     ajax.addParam('sysparm_start_date',startDate);
     ajax.addParam('sysparm_end_date',endDate);
     ajax.addParam('sysparm_schedule_id',scheduleID);
     ajax.getXMLWait();
     var answer = ajax.getAnswer();
     if(answer == 'true'){

     }
     else{
        g_form.showErrorBox("u_maint_window", "Start/End dates are not within the specified change window.");
     }
  }

} </source> System Script Include:

Note
Note: These API calls changed in the Calgary release:
  • GlideDateTime replaces Packages.com.glide.glideobject.GlideDateTime
  • GlideSchedule replaces Packages.com.glide.schedules.Schedule

The new script object calls apply to the Calgary release and beyond. For releases prior to Calgary, substitute the packages calls as described above. Packages calls are not valid beginning with the Calgary release. For more information, see Scripting API Changes.

<source lang="javascript"> var MySysScriptInclude = Class.create();

MySysScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {

globalCheckWindows: function(){

var sDate = this.getParameter('sysparm_start_date'); var eDate = this.getParameter('sysparm_end_date'); var scheduleID = this.getParameter('sysparm_schedule_id');

var start_date = new GlideDateTime(); start_date.setDisplayValue(sDate); var end_date = new GlideDateTime(); end_date.setDisplayValue(eDate);

var wall_clock_time = parseInt(gs.dateDiff(sDate, eDate, true)); gs.log('Wall clock duration: ' + wall_clock_time);

var g = new GlideRecord('cmn_schedule'); g.addQuery('sys_id', scheduleID); g.query(); var answer = 'false';

while (g.next()) { var sched = new GlideSchedule(g.sys_id); var dur = sched.duration(start_date, end_date); var schedule_time = parseInt(dur.getNumericValue() / 1000, 10); gs.log('Schedule duration: ' + schedule_time); if (wall_clock_time == schedule_time){ answer = 'true'; } else{ answer = 'false'; break; } } return answer; }</source>