ServiceNow Scripting Essentials Overview

Sep 14, 2024

Lecture Notes on ServiceNow Scripting

Overview of Scripting in ServiceNow

  • Key Concepts:
    • GlideRecord: Used to interact with the database.
    • GlideAjax: Allows client-side scripts to call server-side scripts (Script Includes).
    • Script Includes: Reusable server-side scripts that can be called from other scripts.

GlideRecord

  • Used to perform CRUD operations on ServiceNow tables.
  • Example usage:
    var gr = new GlideRecord('incident');
    gr.addQuery('active', true);
    gr.query();
    while (gr.next()) {
        // Process each record
    }
    

GlideAjax

  • Used to call Script Includes from client-side scripts.
  • Example usage:
    var ga = new GlideAjax('YourScriptIncludeName');
    ga.addParam('sysparm_name', 'functionName');
    ga.getXMLAnswer(function(response) {
        // Handle response
    });
    

Script Includes

  • Can be defined as client callable or server-side only.
  • Structure:
    var YourScriptIncludeName = Class.create();
    YourScriptIncludeName.prototype = {
        initialize: function() {},
        yourFunction: function() {
            // Your logic here
        },
        type: 'YourScriptIncludeName'
    };
    

Client Scripts

  • Types:
    • onLoad: Runs when the form loads.
    • onChange: Runs when a field value changes.
    • onSubmit: Runs when the form is submitted.

Business Rules

  • Run logic on the server-side when database actions occur (insert, update, delete).
  • Types:
    • Before: Runs before the record is saved.
    • After: Runs after the record is saved.
    • Async: Runs asynchronously after the record is saved.
    • Query: A condition to query the records.
  • Example:
    (function executeRule(current, previous /*null when async*/) {
        // Business logic here
    })(current, previous);
    

Access Control Lists (ACLs)

  • Control access to records, fields, and tables.
  • Types:
    • Table-level ACLs: Control access to entire tables.
    • Field-level ACLs: Control access to specific fields in a table.
  • Example condition:
    • Only allow users with a specific role to access certain records.

Display Business Rules

  • Specific type of business rule that runs when a form is displayed to the user.
  • Used to set field values or perform actions when a form loads.

Example Scenarios

  • Duplicate Request Validation:
    • Using GlideAjax to call a Script Include to check for duplicate requests.
  • User Permissions:
    • Using ACLs to restrict access based on user roles and active status.

Important Functions & Methods

  • getValue(): Retrieves the current value of a field.
  • setValue(): Sets a new value for a field.
  • addQuery(): Adds a query condition to a GlideRecord.
  • addEncodedQuery(): Adds a query condition using a string format.

Conclusion

  • Understanding the use of GlideRecord, GlideAjax, Script Includes, Business Rules, ACLs, and UI Policies is crucial for effective ServiceNow scripting.
  • Practice using these concepts to solidify understanding.