I am trying to delete 5500 orphaned TASK records that have No Assignment Group, No Assigned to and No Parent.

Cory Miller
Giga Expert

Hello,

I am trying to delete 5500 TASK records that have No Assignment Group, No Assigned to and No Parent. 

var rec = new GlideRecord('task');
rec.query();
while (rec.next()) {
if (rec.number.startsWith('TASK') && rec.assigned_to.isNull() && rec.opened_by == '64c0bfbcacc95000ac148fcc9e538060' &&
rec.assignment_group.isNull()) {
gs.print(rec.number + ' deleted');
rec.deleteRecord();
}

It worked with just one condition but was deleting records from all apps that extended the Task table.

I am only looking to delete orphaned TASKs, that have no Parent (RITM)

Not sure if the above syntax is correct.

This did work:  But I was not able to understand the results. A manual query showed data was deleted, but I am seeing that it deleted additional data that was not opened by the user that I set the query for.

var rec = new GlideRecord('task');
rec.query();
while (rec.next()) {
if (rec.assigned_to == '' && rec.opened_by == '64c0bfbcacc95000ac148fcc9e538060' && rec.assignment_group == '') {
rec.deleteRecord();
  }
}

 

1 ACCEPTED SOLUTION

What I came here to say, but had to scroll through a thread of replies lol. I think sc_task instead of task is your answer.

View solution in original post

8 REPLIES 8

Ok. Lets use your script. I just added few more condition to your condition. This should work. 

I am not sure, why you are using opened_by in condition. Do you want to delete task only created by a particular user?

 

var rec = new GlideRecord('task');
rec.query();
while (rec.next()) {
if (rec.parent=='' && rec.assigned_to == '' && rec.opened_by == '64c0bfbcacc95000ac148fcc9e538060' && rec.assignment_group == '' && rec.number.indexOf('TASK')>-1) {

gs.print(rec.number + ' deleted'); 
rec.deleteRecord();
   }
}


Please mark this response as correct or helpful if it assisted you with your question.

Yes, only created by a certain user with no assignment group and no assigned to

why not query on sc_task table instead of task? just curious.

What I came here to say, but had to scroll through a thread of replies lol. I think sc_task instead of task is your answer.