David Pichard
Mega Guru

//See if any closure records exist
//--------------------------------------------------------------
var ct = new GlideRecord('u_ks_task_closure');
ct.query();
while(ct.next()){

// If closure record end date is past today then add task record
//---------------------------------------------------------------
if(new Date().getTime() > new Date(ct.u_closure_date.substring(0,4), (ct.u_closure_date.substring(5,7)-1), ct.u_closure_date.substring(8,10)).getTime()){

  //Find RITM record, change it's status to in progress and add a task
  //---------------------------------------------------------------
  var ritm = new GlideRecord('sc_req_item');
    if(ritm.get('number',ct.u_ritm_number)){
   
        //If ritm wasn't rejected update it's status
        //-----------------------------------------
        if(ritm.approval != 'rejected'){
          ritm.active = true;
          ritm.state = 2;
          ritm.stage = "executing"
          ritm.comments = "New task added to remove access since end date ("+ct.u_closure_date+") has passed.";
          ritm.update();
        }else{
          continue;
        }  
    }

  //Get assignment group for this particular task/RITM
  //---------------------------------------------------------------
  var assignGroup = "";
  var group = new GlideRecord('u_ks_task_closure_helper_table');
  if(group.get('u_catalog_item',ritm.cat_item.sys_id)){
      assignGroup = group.u_task_closure_group.sys_id;
  }

  // Create new Task record and tie it to the parent ritm record
  //---------------------------------------------------------------
  var newTask = new GlideRecord('sc_task');
  newTask.initialize();
  newTask.short_description = "REMOVE ACCESS: '"+ritm.cat_item.name+"' for "+ritm.request.requested_for.name;
  newTask.description = "Access to this resource has expired and needs to be removed";
  newTask.assignment_group = assignGroup;//fulfillment group from helper table
  newTask.request_item = ritm.sys_id;//sys id of parent ritm
  newTask.parent = ritm.sys_id;//sys id of parent ritm
  newTask.insert();

  // Delete record from closure table
  //---------------------------------------------------------------
  ct.deleteRecord();  


}//End if closure end date is past today

}//end loop

2 Comments
solutioningnow
Giga Guru

Good thought


David Pichard
Mega Guru

Mahalo. The purpose of this script/job is to look for RITM records that have an end date as included with a variable set. The job runs each night and looks at the end date of these specific RITMs that have been added to a custom table on submission. If the end date of the RITM has passed, this job re-opens the RITM, creates a custom task to remove access to the resource requested in the RITM, and assigns it to the appropriate group.   This is just the code portion of the scheduled job. There is also a business rule that is a part of this solution that populates the custom table with info about the RITM record.