Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var GlideRecordHelper = Class.create();
GlideRecordHelper.prototype = {
initialize: function(tableName) {
if (!tableName) {
throw new Error("Table name is required.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is your reason to handle the error in this way?

}
this.tableName = tableName;
},

getRecords: function(queryObj) {
var gr = new GlideRecord(this.tableName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to go the wrapper route, it would make sense to add additional error handling, security, and input validations to make it more robust.

if (!gr.isValid()) {
throw new Error("Invalid table name: " + this.tableName);
}

for (var key in queryObj) {
if (queryObj.hasOwnProperty(key)) {
gr.addQuery(key, queryObj[key]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect error handling here as well. If an object value is now empty it will query on empty resulting in unexpected behavior that will be difficult to spot.

}
}

gr.query();
var results = [];
while (gr.next()) {
results.push(gr);
}
return results;
},

type: 'GlideRecordHelper'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# GlideRecordHelper for ServiceNow

A simple utility class to help ServiceNow developers query records using GlideRecord with cleaner syntax.

## How to Use


var helper = new GlideRecordHelper('incident');
var records = helper.getRecords({ priority: 1, active: true });

records.forEach(function(record) {
gs.info(record.number);
});

input:

var helper = new GlideRecordHelper('incident');
var records = helper.getRecords({ priority: 1, active: true });

records.forEach(function(record) {
gs.info(record.number);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if, based on the GlideRecord we did, we want to update one or multiple values on the record?

});

Output:

x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
x_snc_hack4good_0: INC0000055
Loading