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

Mike,



When looking for empty values you should use object.addNullQuery('field_name');.   So in your script you should change:



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



to:



SCTask.addNullQuery('u_work_complete');



-Steve


Mike Edmonds1
Kilo Expert

This is the code you have helped me create. It seems to be working great! Millions of thanks!



(function(){  


  var reqitem = new GlideRecord('sc_req_item');  


  reqitem.addQuery('stage', 'Fulfillment');   //'Fulfilled'


  reqitem.addNullQuery('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.addNullQuery('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();  


  }  


  }  


  })();