create a macro to get records using sysid?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-11-2025 02:27 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2025 10:26 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2025 05:47 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2025 08:51 AM
Can you give example for each one?