Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

question regarding nested GlideRecord queries

davilu
Mega Sage

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?

13 REPLIES 13

also I see that if (url.indexOf(pft) >= 0) { is missing single quotes around pft



if (url.indexOf('pft') >= 0) {


Hey anil, I believe current is available.   I saw t on another community post and it works when I separate out my methods.   Thanks!


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


Hey sorry yea pft is defined.   Its another table name much like var taskTable.   Thanks!


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');