Updated on: August 1, 2025  |  0

JavaScript in ServiceNow

CASE STUDY 1:πŸ–₯️ JavaScript in ServiceNow: Complete Guide (Basic to Advanced)

Learn how JavaScript powers client-side and server-side scripting in ServiceNow, enabling automation, dynamic forms, and integrations.

🌐 1. Introduction to JavaScript in ServiceNow

JavaScript is the primary programming language used in ServiceNow for customization and automation. It is used both client-side and server-side to:

  • Automate workflows
  • Fetch, manipulate, and update records
  • Create dynamic user interfaces
  • Integrate with external systems

πŸ“Œ 2. Types of JavaScript in ServiceNow

ServiceNow uses JavaScript in two main contexts: Client-side and Server-side scripting.

πŸ–±οΈ A. Client-Side Scripting

Executes in the user's browser for form interactions and portal experiences.

  • Client Scripts (onLoad, onChange, onSubmit, onCellEdit)
  • UI Policies with optional scripts
  • GlideForm (g_form) API for form manipulation

// Example: Auto-fill "Category" when "Configuration Item" changes
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    g_form.setValue('category', 'Hardware');
}
    

Common g_form methods:

  • g_form.setValue('field', 'value')
  • g_form.setMandatory('field', true)
  • g_form.addInfoMessage('Message')

βš™οΈ B. Server-Side Scripting

Executes on the ServiceNow server for data processing, automation, and integrations.

  • Business Rules - Triggered before/after DB actions
  • Script Includes - Reusable server-side code
  • Scheduled Jobs - Automate background tasks
  • Glide APIs like GlideRecord, GlideSystem

// Example: Auto-assign incident based on category
(function executeRule(current, previous) {
    if (current.category == 'Hardware') {
        current.assignment_group = 'Hardware Support';
    }
})(current, previous);
    

⚑ 3. Client-Server Communication with GlideAjax

GlideAjax allows client scripts to securely call server-side Script Includes asynchronously.

Client Script


function onLoad() {
   var ga = new GlideAjax('UserUtils');
   ga.addParam('sysparm_name', 'getManager');
   ga.addParam('sysparm_user', g_form.getValue('caller_id'));
   ga.getXMLAnswer(function(response) {
       g_form.setValue('manager', response);
   });
}
    

Script Include


var UserUtils = Class.create();
UserUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getManager: function() {
        var userId = this.getParameter('sysparm_user');
        var gr = new GlideRecord('sys_user');
        if(gr.get(userId)) {
            return gr.manager.name.toString();
        }
        return '';
    }
});
    

πŸš€ 4. Advanced JavaScript Concepts in ServiceNow

  • Asynchronous Business Rules for long-running processes
  • Scoped Application Scripts with proper logging
  • Custom Events using gs.eventQueue()
  • Scripted REST APIs to expose ServiceNow data
  • Security Practices: ACLs and input validation

// Example: Scheduled Job to close resolved incidents after 7 days
var gr = new GlideRecord('incident');
gr.addQuery('state', 'Resolved');
gr.addQuery('sys_updated_on', '<=', gs.daysAgo(7));
gr.query();
while (gr.next()) {
    gr.state = 'Closed';
    gr.update();
}
    

CASE STUDY 2:πŸ’‘ Best Practices

  • Use Script Includes for reusable logic.
  • Run heavy processing asynchronously.
  • Test scripts in sub-production environments.
  • Use gs.info() or gs.log() for debugging.
  • Keep Client Scripts light to avoid performance issues.

🎬 Conclusion

JavaScript is the core scripting language in ServiceNow, powering client-side interactions, server-side automation, and integrations. By mastering:

  • Client Scripts & GlideForm
  • Business Rules & Script Includes
  • Advanced APIs like GlideAjax & Scripted REST

…you can build powerful, scalable, and automated ServiceNow applications that streamline business processes. βœ…

Comments

No comments yet.


Log in to post a comment