create a macro to get records using sysid?

Ramakrishna Rao
Tera Contributor
 
3 REPLIES 3

pavankenche
Tera Contributor

@Ramakrishna Rao 

To create a macro in ServiceNow that fetches records using a Sys ID, you can either create a Script Include + UI Macro, or for simpler use, use a Script Include + GlideRecord query in a UI Action or custom widget.

If you're looking for a macro-style reusable code snippet, here's a straightforward example using a Script Include that you can call from other places (like a UI Action or Business Rule):

✅ 1. Script Include: Get Record by Sys ID

Create a Script Include like this:

var GetRecordBySysId = Class.create();
GetRecordBySysId.prototype = {
    initialize: function() {},

    getRecord: function(tableName, sysId) {
        if (!tableName || !sysId) return null;
        
        var gr = new GlideRecord(tableName);
        if (gr.get(sysId)) {
            return gr;
        }
        return null;
    },

    type: 'GetRecordBySysId'
};
  • Set Client Callable to false unless you need it client-side via GlideAjax.

✅ 2. Example Usage in Script (Server-side)

var recordFetcher = new GetRecordBySysId();
var rec = recordFetcher.getRecord('incident', '47f9960b1bd55c503234dc7fde4bcb68');

if (rec) {
    gs.info('Short Description: ' + rec.short_description);
} else {
    gs.info('Record not found');
}

 

If you found this helpful, please mark it as helpful!

Vishal Jaswal
Giga Sage

Hello @Ramakrishna Rao 

If you can elaborate as what you are trying to achieve:

• Reusable server-side logic → use Script Include
• UI formatting/display → use UI Macro
• Client-side usage (e.g., in Catalog UI) → you might want a Scripted REST API or GlideAjax


Hope that helps!

Can you give example for each one?