-
Notifications
You must be signed in to change notification settings - Fork 908
GlideRecord Query Helper #1754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GlideRecord Query Helper #1754
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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."); | ||
| } | ||
| this.tableName = tableName; | ||
| }, | ||
|
|
||
| getRecords: function(queryObj) { | ||
| var gr = new GlideRecord(this.tableName); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
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?