Skip to main content

ServiceNow Editor Macros

 ServiceNow Editor Macros


Editor Macros are a powerful feature in ServiceNow that allows you to save time and write cleaner, more efficient code. In this post, I share an example of creating an editor macro for one of the most common code blocks you may find yourself writing: a GlideAjax call. 🤖💻

What is GlideAjax?

GlideAjax is a client-side API in ServiceNow that allows you to make asynchronous calls to server-side scripts and retrieve data from them without requiring a page reload. It enables you to build dynamic and responsive web interfaces that interact with server-side logic without having to reload the entire page.

GlideAjax works by invoking server-side scripts or business rules using an AJAX-style request. It takes the following parameters:

  • The name of the server-side script or business rule to be executed
  • Parameters to be passed to the script or business rule
  • A callback function to handle the response from the server

Allen Andreas has a really great explainer video on GlideAjax, and you can consult the official ServiceNow documentation as well for more information.

To The Macro

Macros are a simple but powerful way for ServiceNow developers to speed up their everyday tasks and build scripts more efficiently using reusable code blocks. As an example, and the topic of this post, a macro could be created for a GlideAjax call. The macro, named "getRecordAjax", would contain the full script for the GlideAjax call using templated language so all you have to change is the template rather than write the same lines of code over and over when you need to make a server-side call!

To get started, navigate to System Definition > Syntax Editor Macros

No alt text provided for this image

As you’ll see, ServiceNow gives you some editor macros out of the box you can use including a for loop, a GlideRecord query, or everyone’s favorite - documenting code!

No alt text provided for this image

Click New and give your Macro a name - in this case getRecordAjax - and a comment or two about the purpose/use case of the macro.

In the text field is where you will put your code snippet that should generate when you invoke the macro.

var getRecordAjax = new GlideAjax('MyScriptInclude');
getRecordAjax.addParam('sysparm_name', 'myFunction');
getRecordAjax.getXML(function (response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");;

Save this record and head over to somewhere you can write a script, like a Client Script where you would commonly make an Ajax call. In the Script field start typing the name of your editor macro and you’ll notice that you are given an intellisense result matching the name. Now, simply press Tab and watch as your full block of code populates in front of your eyes!

Pretty neat stuff, huh? As you can imagine, this simple tip can help save developers time and reduce errors when they’re writing code by giving an easy-to-remember shortcut!

#ServiceNow #EditorMacros #Automation #Development #CodeEfficiency #CustomFunctionality


Popular posts from this blog

URL link in addInfoMessage

var ga=new GlideAjax('gld_HR_ajax'); ga.addParam('sysparm_name', 'checkEmployeeNumber_hrProfile'); ga.addParam('sysparm_hrprofilenumber', g_form.getValue('number')); ga.addParam('sysparm_employeenumber', newValue); ga.getXMLAnswer(function(answer) { if (answer!='undefined' && answer!=''){ var navURL="<a style='text-decoration:underline;color:blue'" + answer + ">" + answer + "</a><img width='3' src='images/s.gif'/>"; var sMsg='The employee number entered already exists on another HR Profile ' + navURL; //alert(sMsg); g_form.showErrorBox('employee_number', 'error - please check'); g_form.addInfoMessage(sMsg); } });

GlideRecord setValue

setValue(String name, Object value) Sets the specified field to the specified value. Normally a script would do a direct assignment, for example,  gr.category = value . However, if in a script the element name is a variable, then  gr.setValue(elementName, value)  can be used. When setting a value, ensure the data type of the field matches the data type of the value you enter. This method cannot be used on journal fields. If the value parameter is null, the record is not updated, and an error is not thrown!/api_doc?v=madrid&id=r_GlideRecord-setValue_String_Object