This is the first in hopefully a series of articles on incorporating useful functional programming techniques while writing JavaScript in ServiceNow.
How many times have you seen code like the following?
var grIncident = new GlideRecord('incident');
grIncident.addQuery('active=true^state=1');
grIncident.query();
while (grIncident.next()) {
// do some processing with the GlideRecord
gs.info('processing ' + grIncident.getDisplayValue());
}
This boilerplate code is so common in the platform, you probably use a code snippet or macro to type everything for you, then go back and edit as needed.
Any time you see the same code over and over, an alarm should go off in your head. You may have heard the about the DRY (Don't Repeat Yourself) principle. Why is it then, that we as ServiceNow developers violate this design principle virtually every day when we repeat this boilerplate code over and over again?
If you use such a code snippet, what are the things you end up going back to edit? Most likely they include the table name, query string and the processing to be done.
This gives us a hint to what things are fixed and what things change. In software design, the best practice is to separate the things that change from the things that remain the same. An example of this is using system properties to enable changes to code behavior without actually changing the code.
Can we write the GlideRecord query with looping logic in such a way that we separate the things we change each time from the boilerplate stuff that remains same?
Yes, we can, and it is the first step we'll take toward using a functional approach for our query logic. Here's a helper function named query that takes three parameters - the very three things we change after using a typical GlideRecord query code snippet:
function query(table, query, fn) {
var gr = new GlideRecord(table);
gr.addQuery(query);
gr.query();
while (gr.next()) {
fn(gr);
}
}
Here's an example of using our query helper to process new incidents:
function processNewIncident(grIncident) {
gs.info('processing ' + grIncident.getDisplayValue());
// do some processing with the GlideRecord
}
query('incident', 'active=true^state=1', processNewIncident);
Here's an example of re-using our helper function for something completely different:
function processClosedHighPriorityIncident(grIncident) {
gs.info('processing ' + grIncident.getDisplayValue());
}
query('incident',
'active=false' +
'^ priority=1 ^OR priority=2',
processClosedHighPriorityIncident);
No more repeating boilerplate code, and by encapsulating our processing of an individual GlideRecord into a separate function, separating the processing concern from the data retrieval concern (another best practice - Separation of Concerns or SoC) our code becomes more readable and maintainable.
How many times have you seen code like the following?
var grIncident = new GlideRecord('incident');
grIncident.addQuery('active=true^state=1');
grIncident.query();
while (grIncident.next()) {
// do some processing with the GlideRecord
gs.info('processing ' + grIncident.getDisplayValue());
}
This boilerplate code is so common in the platform, you probably use a code snippet or macro to type everything for you, then go back and edit as needed.
Any time you see the same code over and over, an alarm should go off in your head. You may have heard the about the DRY (Don't Repeat Yourself) principle. Why is it then, that we as ServiceNow developers violate this design principle virtually every day when we repeat this boilerplate code over and over again?
If you use such a code snippet, what are the things you end up going back to edit? Most likely they include the table name, query string and the processing to be done.
This gives us a hint to what things are fixed and what things change. In software design, the best practice is to separate the things that change from the things that remain the same. An example of this is using system properties to enable changes to code behavior without actually changing the code.
Can we write the GlideRecord query with looping logic in such a way that we separate the things we change each time from the boilerplate stuff that remains same?
Yes, we can, and it is the first step we'll take toward using a functional approach for our query logic. Here's a helper function named query that takes three parameters - the very three things we change after using a typical GlideRecord query code snippet:
function query(table, query, fn) {
var gr = new GlideRecord(table);
gr.addQuery(query);
gr.query();
while (gr.next()) {
fn(gr);
}
}
Here's an example of using our query helper to process new incidents:
function processNewIncident(grIncident) {
gs.info('processing ' + grIncident.getDisplayValue());
// do some processing with the GlideRecord
}
query('incident', 'active=true^state=1', processNewIncident);
Here's an example of re-using our helper function for something completely different:
function processClosedHighPriorityIncident(grIncident) {
gs.info('processing ' + grIncident.getDisplayValue());
}
query('incident',
'active=false' +
'^ priority=1 ^OR priority=2',
processClosedHighPriorityIncident);
No more repeating boilerplate code, and by encapsulating our processing of an individual GlideRecord into a separate function, separating the processing concern from the data retrieval concern (another best practice - Separation of Concerns or SoC) our code becomes more readable and maintainable.
Best Servicenow Online Training Institute in Hyderabad
ReplyDeleteWe are offering Servicenow On Job Supports for USA Canada students
ReplyDeleteCall / whats app : IND - +91-9642373173 / +91-9966624055 and USA - +1-845-915-8712, email us at info@svtrainings.com
https://www.svtrainings.com/on-job-support.html
This article is very informative thanks for sharing.
ReplyDeleteServiceNow Training in Hyderabad
ServiceNow Training in Ameerpet
ServiceNow Training
ServiceNow Online Training
Interesting information.
ReplyDeleteTo know more about ServiceNow contact us for the best training Mindbox Trainings
http://mindboxtrainings.com/Learn-Servicenow-Online-Training.html
This is an amazing blog, thank you so much for sharing such valuable information with us.
ReplyDeleteServicenow Training
Servicenow Certification
Servicenow Now Learning
Servicenow Online Training
Servicenow Online Training in Hyderabad
Servicenow Developer Training
Servicenow Training in Ameerpet
Servicenow Course Online
Best Servicenow Online Training
Servicenow Developer Training Online
Servicenow Admin Training Online
Learn Servicenow Online
Servicenow Developer Online Training
Servicenow Admin Online Training
Servicenow Online Training in India
Servicenow Online Training Hyderabad
Thanks for sharing
ReplyDeleteservicenow administrator certification
servicenow admin training online