💻

ServiceNow Developer Training

Jul 2, 2024

Lecture Notes: ServiceNow Developer Training

Introduction

  • Instructor: Gaurav Tripathi
  • Experience: 9.5 years in IT, 6.5 years in ServiceNow
  • Objective: Train on ServiceNow scripting for developers

Prerequisites

  1. Experience in ServiceNow Administration (6 months) required.
  2. Knowledge of JavaScript.
  3. Completed Code Academy JavaScript course (recommended).

Topics Covered

  • Client Scripts
  • UI Policies
  • Business Rules
  • Script Includes
  • Workflows
  • GlideAjax

Client-Side Scripting

  • Client-Side Scripts execute within a user’s browser.
  • Manage forms and form fields (e.g., show an alert on form load, validate form data).

Locations to Write Client-Side Scripts

  • Client Scripts
  • UI Policies
  • Service Catalog UI Policies
  • UI Actions
  • UI Context Menus
  • Validation Scripts

Key Client-Side APIs

  1. GlideForm (g_form)
    • Methods: addDecoration, addErrorMessage, addInfoMessage, setMandatory, etc.
  2. GlideUser (g_user)
    • Methods: getUserID, getUserName, hasRole, etc.
  3. GlideAjax (g_ajax)
    • Used to call server-side code from client side.

Scripting in Business Rules

  • Server-side scripting
  • Trigger Points: Insert, Update, Delete, Query
  • Execution Timings: Before, After, Async, Display
  • Key Objects: current, previous, GlideAjax, GlideSystem (gs)

Example Business Rule

var gr = new GlideRecord('incident'); gr.addQuery('active', true); gr.query(); while (gr.next()) { gs.log('Active Incident: ' + gr.number); }

Script Includes

  • Store reusable JavaScript functions
  • Executed on server side

Types of Script Includes

  1. Classless: Single reusable function.
  2. Extended Classes: Call functions on the client side using GlideAjax.
  3. Utils: Classes with multiple functions.

Example Script Include

var DemoUtils = Class.create(); DemoUtils.prototype = { initialize: function() {}, calculateSum: function(a, b) { return a + b; }, type: 'DemoUtils' };

Workflows

  • Automated sequence of activities (e.g., Approvals, Sending Notifications).
  • Workflow Editor: Interface to create/manage workflows
  • Scripting in Workflows: Used in different activities like IF conditions, notifications, REST messages, etc.

Example Workflow Script

if (current.priority == 1) { workflow.scratchpad.issue_priority = 'High'; }

GlideAjax

  • Used to make server-side calls from client side scripts
  • Syntax
var ga = new GlideAjax('ServerScriptInclude'); ga.addParam('sysparm_name', 'getData'); ga.getXMLAnswer(callbackFunction);

Debugging

  • Tools: JavaScript Log, Field Watcher, Response Time Indicator, Try/Catch
  • Syntax for Logging: gs.log('Your message');
  • Catch Errors:
try { // code } catch (ex) { gs.log('Error: ' + ex); }

Conclusion

  • Developer Scope: Ability to perform advanced scripting in various ServiceNow functionalities.
  • Recommendation: Practice the examples, become comfortable with scripting in different scenarios.

Resources

  1. ServiceNow Developer Documentation
  2. Code Academy JavaScript Course
  3. ServiceNow Developer Portal