background script

Mike Edmonds1
Kilo Expert

Looking to do some house keeping. We have to Query all RITM's that are in fulfillment or fulfilled stage and fulfilled (u_fulfilled_time) time is empty. From RITM we need to query the associated catalog tasks (sc_task), where state is 'work complete', work complete (u_work_complete) is empty.

Then we need to close all these tasks just like in schedule job, after closing tasks, we need to check associated RITM's of tasks to see if all other tasks under this RITM is closed.

If all other tasks under RITM is closed, than put RITM stage to completed, active false and comments to "Marked the stage as complete". It would be great if to provide gs.print to get RITM records and task records numbers, as we are doing change in production, so its good to see numbers matching up before closing/updating records. Here is what I have as a background script.

I have commented out the parts where the fields are set until I know the script is pulling the correct records.

// Check for RITMs where state is fulfillment and fulfilled time is empty

    var reqitem = new GlideRecord('sc_req_item');

    reqitem.addQuery('stage', '1');

    //reqitem.addQuery('stage', '5');

    reqitem.addQuery('u_fulfilled_time', " is empty");

    reqitem.query();

   

    while(reqitem.next()) {

// print list of matching RITMs

    gs.print(reqitem.number + " RITM Number");

//Check for and close tasks where Work Complete is empty.

    var SCTask = new GlideRecord('sc_task');

    SCTask.addQuery('request_item', reqitem.sys_id);

    SCTask.addQuery('u_work_complete', " is empty");

    SCTask.addQuery('state','6');

    SCTask.query();

    while (SCTask.next()) {

// print list of matching Tasks

    gs.print(SCTask.number + " Task Number");

                     

/*

//Set task state to closed and update record

              SCTask.state = '3';

                      SCTask.active = 'false';

                      SCTask.update();

*/

   

}

/*    

// Close RITM if all Tasks are closed

          var myvar = 'false';

          var SCTask1 = new GlideRecord('sc_task');

          SCTask1.addQuery('request_item', reqitem.sys_id);

          SCTask1.addQuery('state','!=','3');

          SCTask1.query();

            while (SCTask1.next()) {

                     

                      myvar = 'true';

        }

            if( myvar=='false')

          {

                          reqitem.stage= '7';

                          reqitem.comments= "Marking the stage as Completed.";

                          reqitem.active = 'false'

                          reqitem.update();

}

*/

}

6 REPLIES 6

Abhinay Erra
Giga Sage

Here you go.



(function(){


  var reqitem = new GlideRecord('sc_req_item');


  reqitem.addQuery('stage', 1);


  reqitem.addQuery('u_fulfilled_time','');


  reqitem.query();


  while(reqitem.next()) {


  // print list of matching RITMs


  gs.print(reqitem.number + " RITM Number");


  //Check for and close tasks where Work Complete is empty.


  var SCTask = new GlideRecord('sc_task');


  SCTask.addQuery('request_item', reqitem.sys_id);


  SCTask.addQuery('u_work_complete', '');


  SCTask.addQuery('state',6);


  SCTask.query();


  while (SCTask.next()) {


  // print list of matching Tasks


  gs.print(SCTask.number + " Task Number");


  //Set task state to closed and update record


  SCTask.state = 3;


  SCTask.update();


  }


  var SCTask1 = new GlideRecord('sc_task');


  SCTask1.addQuery('request_item', reqitem.sys_id);


  SCTask1.addQuery('state','!=',3);


  SCTask1.query();


  if(!SCTask1.hasNext()) {


  reqitem.stage= 7;


  reqitem.comments= "Marking the stage as Completed.";


  reqitem.active = false;


  reqitem.update();


  }


  }


  })();


Abhinay, I think we're close. Here is the output from your suggestion:



Time: 0:00:04.265 id: corningdev2_2[glide.8] for: /* corningdev2003, gs:C2ACB9D94F587A00D75E650F0310C7B3, tx:5f9052914f987a00d75e650f0310c7e2 */ SELECT task0.`sys_id` FROM task task0 WHERE task0.`sys_class_name` = 'sc_req_item' AND task0.`stage` = '1' AND task0.`end_date` IS NULL



I'm pretty sure we can't just use (' ') for an empty field. I found a page on it, but cannot ascertain the syntax from the information given.



Using GlideRecord to Query Tables - ServiceNow Wiki


Selects records that do NOT match the pattern in the field. This operator does not retrieve empty fields. For empty values, use the operators "is empty" or "is not empty". The example shown on the right will get all records where the short_description field does not have the word Error.



Abhinay Erra
Giga Sage

You can use. What is the field type of it


Both of the custom fields are Date and Time. Is there any information in the output I provided?