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 03:05 PM
also I see that if (url.indexOf(pft) >= 0) { is missing single quotes around pft
if (url.indexOf('pft') >= 0) {
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2018 03:20 PM
Hey anil, I believe current is available. I saw t on another community post and it works when I separate out my methods. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2018 03:09 PM
Hi David,
Here in the below syntax you have used pft. Have you defined that variable. If pft is a string then use it in quotes and try.
if (url.indexOf(pft) >= 0)
Thanks,
Sagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2018 03:16 PM
Hey sorry yea pft is defined. Its another table name much like var taskTable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2018 06:27 PM
This line of code var url = tasks.getDisplayValue('url'); should come after while loop, like
var tasks = new GlideRecord(taskTable);
tasks.query();
while (tasks.next()) {
var url = tasks.getDisplayValue('url');