Skip to content

Commit 8e55556

Browse files
committed
Add Error Handling Wrapper
1 parent 7f50ab1 commit 8e55556

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# GlideRecord Error Handling Wrapper
2+
3+
## Description
4+
This snippet provides a **safe execution wrapper** around GlideRecord queries.
5+
It helps avoid runtime errors due to invalid table names, malformed queries, or null references, and ensures graceful script continuation.
6+
7+
---
8+
9+
## Note
10+
- Works in global or scoped applications.
11+
- Suitable for Background Scripts, Business Rules, or Script Includes.
12+
- Prevents script termination from invalid GlideRecord calls.
13+
14+
---
15+
16+
## Functionality
17+
1. Validates table existence using `isValid()`.
18+
2. Wraps GlideRecord operations in a `try...catch` block.
19+
3. Logs detailed error messages via `gs.error()`.
20+
4. Allows the developer to safely pass a callback for query operations.
21+
22+
---
23+
24+
## Usage Example
25+
```js
26+
safeQuery('incident', function (gr) {
27+
gr.addQuery('priority', 1);
28+
gr.query();
29+
while (gr.next()) {
30+
gs.info('High-priority Incident: ' + gr.number);
31+
}
32+
});
33+
```
34+
35+
## Output
36+
```
37+
High-priority Incident: INC0012345
38+
High-priority Incident: INC0012346
39+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* GlideRecord Error Handling Wrapper
3+
* Safely executes GlideRecord queries and catches runtime errors such as invalid table names or malformed queries.
4+
* Logs detailed error information and ensures the script continues gracefully.
5+
*/
6+
7+
function safeQuery(table, queryCallback) {
8+
try {
9+
var gr = new GlideRecord(table);
10+
if (!gr.isValid()) {
11+
gs.error('Invalid table name: ' + table);
12+
return;
13+
}
14+
15+
queryCallback(gr);
16+
} catch (e) {
17+
gs.error('Error executing GlideRecord query on table "' + table + '": ' + e.message);
18+
}
19+
}
20+
21+
// Example usage:
22+
safeQuery('incident', function (gr) {
23+
gr.addQuery('active', true);
24+
gr.query();
25+
while (gr.next()) {
26+
gs.info('Incident: ' + gr.number);
27+
}
28+
});

0 commit comments

Comments
 (0)