question regarding nested GlideRecord queries
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2018 02:10 PM
I'm putting together a script includes to close certain tasks and am running into an issue with nested GlideRecord queries. A snippet of my code is below:
var close_tasks= Class.create();
close_tasks.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
closeTasks: function(item) {
var taskTable = 'x_dnf_task';
var tasks = new GlideRecord(taskTable);
var url = tasks.getDisplayValue('url');
tasks.query();
while (tasks.next()) {
if (url.indexOf('c.table_') == -1) {
tasks = new GlideRecord(taskTable);
tasks.addQuery('assigned_to', current.ref_user);
tasks.addQuery('short_description', 'CONTAINS', item);
tasks.addQuery('state', 'NOT IN', '3,4,7');
tasks.query();
while (tasks.next()) {
tasks.setValue('state', 3);
tasks.setValue('closed_at', new GlideDateTime());
tasks.update();
}
} else {
if (url.indexOf(pft) >= 0) {
tasks = new GlideRecord(taskTable);
tasks.addQuery('assigned_to', current.ref_user);
tasks.addQuery('url', 'CONTAINS', item);
tasks.query();
while (tasks.next()) {
tasks.setValue('state', 3);
tasks.setValue('closed_at', new GlideDateTime());
tasks.update();
}
}
}
}
},
type: 'close_tasks'
});
The first part of the main if statement runs perfectly, but once I get to the else portion, the code doesn't work. I've separated out the above code into two separate methods and it works great, was just wondering if I could combine them together.
What is wrong with my syntax above? Do I need different variable names when querying the GlideRecord again?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2018 06:37 PM
Modified the code little bit here, please check if this helps.
var close_tasks= Class.create();
close_tasks.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
closeTasks: function(item) {
var taskTable = 'x_dnf_task';
var tasks = new GlideRecord(taskTable);
tasks.query();
while (tasks.next()) {
var url = tasks.getDisplayValue('url');
var tasks1 = new GlideRecord(taskTable);
tasks1.addQuery('assigned_to', current.ref_user);
tasks1.addQuery('short_description', 'CONTAINS', item);
if (url.indexOf('c.table_') == -1) {
tasks1.addQuery('state', 'NOT IN', '3,4,7');
tasks1.query();
}
else if(url.indexOf(pft) >= 0)
tasks1.query();
while (tasks1.next()) {
tasks1.setValue('state', 3);
tasks1.setValue('closed_at', new GlideDateTime());
tasks1.update();
}
}
},
type: 'close_tasks'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2018 06:46 PM
I think there is an inherit problem with the logic of the code.
From what I can gather, it is querying the system for every 'url', then running one of two queries if there is one instance of a URL containing something.
It just isn't making sense to me when the nested queries have absolutely no dependency on the urls that come from the first query. And you will be re-running the same query over and over.
I have refactored the code below so it does the same thing, but it isn't making sense to me.
var close_tasks= Class.create();
close_tasks.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getURLs: function() {
var urls = [];
var ga = new GlideAggregate('x_dnf_task');
ga.groupBy('url');
ga.query();
while(ga.next()){
urls.push(ga.url.getDisplayValue());
}
return urls;
},
closeTasks: function(item) {
var urls = this.getURLs();
var pft = '???';
for (var i = 0;i<urls.length;i++) {
var tasks2 = new GlideRecord(taskTable);
tasks2.addQuery('assigned_to', current.ref_user);
var url = urls[i];
if (url.indexOf('c.table_') == -1) {
tasks2.addQuery('short_description', 'CONTAINS', item);
tasks2.addQuery('state', 'NOT IN', '3,4,7');
tasks2.query();
} else if (url.indexOf(pft) >= 0 ) {
tasks2.addQuery('url', 'CONTAINS', item);
tasks2.query();
}
while (tasks2.next()) {
this.closeTask(tasks2);
}
}
},
closeTask: function(grTask) {
grTask.setValue('state', 3);
grTask.setValue('closed_at', new GlideDateTime());
grTask.update();
},
type: 'close_tasks'
});
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2018 07:00 PM
Yeah, I just noticed now there is no dependency between the nested GlideRecord queries, we may add below code if we have to make the connection between them but agree intention is not much clear.
tasks1.addQuery('sys_id', tasks.getUniqueValue())

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2018 05:20 PM
Hi David,
Are you able to describe the outcome of what the code is trying to do?
I'm struggling to understand the intend - which tasks are you trying to close?
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022