Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Need help with this script, trying to delete records from a user table that fit a condition

nico2
Giga Expert

Good afternoon, 

Need help with this script, trying to delete records from a user table that fit a condition. But record count displays zero.

Condition that I set is records that were created  on 04/18/2019. 

I have the following, I have not added the delete statement making sure I get the right count first. 

Any ideas are welcome. 

 

removechecksjan();

function removechecksjan() {
var gr = new GlideRecord("u_check_drawer");
gr.addEncodedQuery('sys_created_on=2019-04-18');
gr.query();
var deleteCount = 0;
while(gr.next()){
gr.setWorkflow(false);
deleteCount++;
}
gs.print('Records Deleted: '+ deleteCount);
}

 

1 ACCEPTED SOLUTION

Hi Nico,

Although I usually like to use 'deleteMultiple()' for efficiency's sake, in your case I would not.  If you read through the reference on GlideRecord, there are a couple of counterindications:

  • Your table is named 'u_check_drawer', so I'm guessing there may be some currency values involved.  From the reference they state:
    "Do not use deleteMultiple() on tables with currency fields. Always delete each record individually"
  • Also, I noted you are employing 'setWorkflow(false)', from the ref this will not apply to cascade deletes:
    "Note: The setWorkflow() method is ignored when subsequently using either the deleteProblem() or deleteMultiple() methods to cascade delete."

 

Thanks,

-Brian

View solution in original post

8 REPLIES 8

You are Absolutely spot on. 

 


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

nico2
Giga Expert

Thank you all for your help. I tested the final code that was recommended and I received the correct count that will be deleted.  

Now for the final phase should I use deleteRecord() or deleteMultiple () ?

do I still use gr.next ?

 

Let me know your opinion

 

removechecksjan();

function removechecksjan() {
var gr = new GlideRecord("u_check_drawer");
//gr.addEncodedQuery('sys_created_on=2019-04-18');
gr.addEncodedQuery("sys_created_onON2019-04-18@javascript:gs.dateGenerate('2019-04-18','start')@javascript:gs.dateGenerate('2019-04-18','end')");
gr.query();
var deleteCount = 0;
while(gr.next()){
gr.setWorkflow(false);
deleteCount++;
}
gs.print('Records Deleted: '+ deleteCount);
}

Hi Nico,

Although I usually like to use 'deleteMultiple()' for efficiency's sake, in your case I would not.  If you read through the reference on GlideRecord, there are a couple of counterindications:

  • Your table is named 'u_check_drawer', so I'm guessing there may be some currency values involved.  From the reference they state:
    "Do not use deleteMultiple() on tables with currency fields. Always delete each record individually"
  • Also, I noted you are employing 'setWorkflow(false)', from the ref this will not apply to cascade deletes:
    "Note: The setWorkflow() method is ignored when subsequently using either the deleteProblem() or deleteMultiple() methods to cascade delete."

 

Thanks,

-Brian

nico2
Giga Expert

Thank you all for your help, tested the script in my dev environment.  Deployment/validation successful.