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 02:25 PM
Exactly.. the inner gliderecord variables ( in both the if blocks) should be a new variables.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2018 02:26 PM
Hi David,
The short answer is, yes, you should use a different variable name for your nested query. I'm not sure exactly what you are trying to do, but I'm thinking you could accomplish the same without the nesting. Have you written out in pseudo code the steps to accomplish what you want for your solution? It looks like you are getting all records from the table, then re-querying them to look for the url condition, then closing the ticket.
Thanks,
Laurie
Please mark Correct, Helpful, or Like as applicable!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2018 02:51 PM
Thanks lauriemarlowe and avaranasi! I just tried the following and it still doesn't work:
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) {
var tasks2 = new GlideRecord(taskTable);
tasks2 .addQuery('assigned_to', current.ref_user);
tasks2 .addQuery('short_description', 'CONTAINS', item);
tasks2 .addQuery('state', 'NOT IN', '3,4,7');
tasks2.query();
while (tasks2.next()) {
tasks2.setValue('state', 3);
tasks2.setValue('closed_at', new GlideDateTime());
tasks2.update();
}
} else {
if (url.indexOf(pft) >= 0) {
var tasks3 = new GlideRecord(taskTable);
tasks3.addQuery('assigned_to', current.ref_user);
tasks3.addQuery('url', 'CONTAINS', item);
tasks3.query();
while (tasks3.next()) {
tasks3.setValue('state', 3);
tasks3.setValue('closed_at', new GlideDateTime());
tasks3.update();
}
}
}
}
},
type: 'close_tasks'
});
We have a bunch of tasks that involve filling out forms or scheduling appointments and want to use this script includes to close down tasks once an entry has been inserted or updated on each of the form/appt tables. I want to query the task table and based on its task type, close it down after insert or update. We have business rules on each of these tables that call upon this script includes after insert/update. Any suggestions on how to fix the above code?
Also, my original code separated out into separate methods and it works fine, but I am just curious if I can combine them all into one method. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2018 03:03 PM
wondering..
can a script include access current object without it being passed as a function parameter to it?
I see current in tasks2 .addQuery('assigned_to', current.ref_user); and tasks3.addQuery('assigned_to', current.ref_user);