You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
> Safely fetch a GlideRecord by `sys_id` and log clearly if not found.
4
+
5
+
**Use case:**
6
+
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.
7
+
8
+
```javascript
9
+
/**
10
+
* safeGetBySysId(table, sysId)
11
+
* Returns GlideRecord object or null if not found.
12
+
*/
13
+
functionsafeGetBySysId(table, sysId) {
14
+
if (!table ||!sysId) {
15
+
gs.warn('safeGetBySysId: Missing parameters.');
16
+
returnnull;
17
+
}
18
+
19
+
var gr =newGlideRecord(table);
20
+
if (gr.get(sysId)) {
21
+
return gr;
22
+
}
23
+
24
+
gs.warn('safeGetBySysId: No record found in '+ table +' for sys_id '+ sysId);
25
+
returnnull;
26
+
}
27
+
28
+
// Example:
29
+
var inc =safeGetBySysId('incident', '46d12b8a97a83110eaa3bcb51cbb356e');
0 commit comments