Skip to main content

ServiceNow Editor Macros

 ServiceNow Editor Macros


LinkedIn



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");
    gs.info(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


Comments

Popular posts from this blog

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){

Get URL Parameter - server side script (portal or classic UI)

Classic UI : var sURL_editparam = gs . action . getGlideURI (). getMap (). get ( ' sysparm_aparameter ' ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); } Portal : var sURL_editparam = $sp . getParameter ( " sysparm_aparameter " ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); }