From 675e853c6d0f96f3d385af3d4d0efe3d8c194bc1 Mon Sep 17 00:00:00 2001 From: aniketchavan-blip Date: Thu, 30 Oct 2025 22:30:36 +0530 Subject: [PATCH 1/3] Create safeGetBySysId utility for safe record retrieval This utility function simplifies fetching a GlideRecord by sys_id with built-in null checks and clear logging. It prevents repeated boilerplate code and ensures that missing records are clearly logged in sys.log. Includes an example usage for fetching an incident. --- -snippets/glide-helpers/safeGetBySysId.md | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 -snippets/glide-helpers/safeGetBySysId.md diff --git a/-snippets/glide-helpers/safeGetBySysId.md b/-snippets/glide-helpers/safeGetBySysId.md new file mode 100644 index 0000000000..f617813e50 --- /dev/null +++ b/-snippets/glide-helpers/safeGetBySysId.md @@ -0,0 +1,30 @@ +# safeGetBySysId + +> Safely fetch a GlideRecord by `sys_id` and log clearly if not found. + +**Use case:** +I often need to fetch a single record inside background scripts or Script Includes and don’t want to keep repeating the same `gr.get()` + null checks. This helper makes it a one-liner and keeps logs human-readable. + +```javascript +/** + * safeGetBySysId(table, sysId) + * Returns GlideRecord object or null if not found. + */ +function safeGetBySysId(table, sysId) { + if (!table || !sysId) { + gs.warn('safeGetBySysId: Missing parameters.'); + return null; + } + + var gr = new GlideRecord(table); + if (gr.get(sysId)) { + return gr; + } + + gs.warn('safeGetBySysId: No record found in ' + table + ' for sys_id ' + sysId); + return null; +} + +// Example: +var inc = safeGetBySysId('incident', '46d12b8a97a83110eaa3bcb51cbb356e'); +if (inc) gs.info('Found incident: ' + inc.number); From 49da63c9379d34fbd393b4d2a9da91d5c6037cfb Mon Sep 17 00:00:00 2001 From: aniketchavan-blip Date: Thu, 30 Oct 2025 22:34:44 +0530 Subject: [PATCH 2/3] Create debugLogger.md, feat(snippets/glide-helpers): add environment-aware logger, paginated query, and PR note template Environment-aware logging for server/client scripts, preventing production log clutter while easing debugging in sub-prod instances. --- .../snippets/glide-helpers/debugLogger.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 -snippets/glide-helpers/snippets/glide-helpers/debugLogger.md diff --git a/-snippets/glide-helpers/snippets/glide-helpers/debugLogger.md b/-snippets/glide-helpers/snippets/glide-helpers/debugLogger.md new file mode 100644 index 0000000000..5bee611cad --- /dev/null +++ b/-snippets/glide-helpers/snippets/glide-helpers/debugLogger.md @@ -0,0 +1,25 @@ +```markdown +# debugLogger + +> A simple environment-aware logging utility for client and server scripts. + +**Use case:** +In one of my projects, we wanted to log messages only in non-production environments without commenting them out each time. This small helper does that elegantly. + +```javascript +/** + * debugLogger(message, level) + * Logs only if NOT in production. + * Level can be 'info', 'warn', or 'error' + */ +var debugLogger = (function() { + var isProd = gs.getProperty('instance_name') === 'prod-instance'; // change per your instance + return function(msg, level) { + if (isProd) return; + level = level || 'info'; + gs[level]('[DEBUG] ' + msg); + }; +})(); + +// Example usage: +debugLogger('Checking record count...', 'info'); From aaa3ef4f72c7cf99d25b43e43d726103dc73a65b Mon Sep 17 00:00:00 2001 From: aniketchavan-blip Date: Thu, 30 Oct 2025 22:37:29 +0530 Subject: [PATCH 3/3] Create paginatedQuery.md, feat(snippets/glide-helpers): add environment-aware logger, paginated query, and PR note template Safely fetch large GlideRecord datasets in batches to avoid memory/performance issues during bulk operations. --- snippets/glide-helpers/paginatedQuery.md | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 snippets/glide-helpers/paginatedQuery.md diff --git a/snippets/glide-helpers/paginatedQuery.md b/snippets/glide-helpers/paginatedQuery.md new file mode 100644 index 0000000000..eddf628a33 --- /dev/null +++ b/snippets/glide-helpers/paginatedQuery.md @@ -0,0 +1,40 @@ +# paginatedQuery + +> Retrieve GlideRecord results in batches (pagination) to avoid large query memory issues. + +**Use case:** +During a data-cleanup automation, I hit performance issues when querying 20k+ incidents. This helper lets you fetch results in pages (e.g., 100 at a time). + +```javascript +/** + * paginatedQuery(table, query, limit, callback) + * Executes callback for each batch of records. + */ +function paginatedQuery(table, query, limit, callback) { + var offset = 0, hasMore = true; + while (hasMore) { + var gr = new GlideRecord(table); + gr.addEncodedQuery(query); + gr.orderBy('sys_created_on'); + gr.setLimit(limit); + gr.setOffset(offset); + gr.query(); + + var count = 0; + while (gr.next()) { + callback(gr); + count++; + } + + if (count < limit) { + hasMore = false; + } else { + offset += limit; + } + } +} + +// Example: +paginatedQuery('incident', 'active=true', 100, function(gr) { + gs.info('Processing ' + gr.number); +});