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); 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'); 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); +});