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