Close all incidents more than a year old

Shree Nag
Tera Expert

Hello,

Newbie to SN .

I'm trying to write background script to run for one time use only, to CLOSE ALL the incidents, that is more than a year old( 365 days).

Nothing happens when I run it in Test environment. What AM I doing wring?

 

rejectOldApprovalRecord ();


function rejectOldApprovalRecord ()  
 {

  var approval = new GlideRecord("incident");


      approval.addQuery('sys_updated_on' , '<=' , gs.daysAgo(365)); // 5 days ago


      approval.addQuery('state', '!=' "closed');//


      approval.addQuery('group.assignment_group', '');//Searches for REQUETERS MANAGER OR MANAGERS MANAGERS APPROVAL, no group approvals


          approval.query();

   

      while (approval.next()) 
{          


         

approval.state='Closed'; 

gs.comments='Auto Closed for inactivity for more than 365 days';


              gs.log(approval.number + ' ................updated with .......... ' +   approval.state);


              approval.update();


        }


}



 

4 REPLIES 4

Aman Kumar S
Kilo Patron

Use addEncodedQuery with  "active=true^sys_updated_on<javascript:gs.beginningOfOneYearAgo()" and remove all your addqueries

 

Can you clarify what you are searching with this line?

approval.addQuery('group.assignment_group', '')

Best Regards
Aman Kumar

Shree Nag
Tera Expert

Im getting this :

Invalid query string received:null : null
Invalid query string received:null : null

Im clueless, as this is my first time debugging in SN now, as newbie into ity. Please help. I changed teh query as suggested by you.

rejectOldApprovalRecord ();


function rejectOldApprovalRecord ()  
 {

      var approval = new GlideRecord("Task");


      approval.addEncodedQuery ('sys_updated_on' , '<=' , gs.daysAgo(365)); // 5 days ago


      approval.addEncodedQuery ('state', '=', 'Active');//


     // approval.addQuery('group.assignment_group', '');//Searches for REQUETERS MANAGER OR MANAGERS MANAGERS APPROVAL, no group approvals


          approval.query();

   

      while (approval.next()) 
           {          


         

               approval.state='Closed'; 

                       gs.comments='Auto Closed for inactivity for more than 365 days';


              gs.log(approval.number + ' ................updated with .......... ' +   approval.state);


              approval.update();


        }


}

Hi Shree,

Please write following fix script. Don't forgot to mark my answer as correct if that helps.

var gr = new GlideRecord('incident');
gr.addEncodedQuery('sys_updated_on<=javascript:gs.beginningOfOneYearAgo()^active=true');
gr.query();
while(gr.next())
{
gr.state = 'closed';
gr.setWorkflow(false);
gr.update();
}
Please hit like and mark my response as correct if that helps
Regards,
Musab

Anshu_Anand_
Kilo Sage
Kilo Sage

 

This one is efficient way.

var gr = new GlideRecord('incident');
gr.addEncodedQuery('sys_created_on<=javascript:gs.beginningOfOneYearAgo()^stateNOT IN6,7,8');
gr.query();
while(gr.next())
{
gr.state = 'closed'; // you can also put 7 as integer 
gr.setWorkflow(false);
gr.update();
}

You have to query on created an year ago and not closed,cancelled,resolved to minimize no. of transactions

during running of script.

 

If its helpful,please mark answer as correct

Regards,
Anshu