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

Prateek kumar
Giga Sage
removechecksjan();


function removechecksjan() {
var gr = new GlideRecord("u_check_drawer");

gr.addEncodedQuery("sys_created_onON2019-04-18@javascript:gs.dateGenerate('2019-04-18','start')@javascript:gs.dateGenerate('2019-04-18','end')");

gr.query();
while(gr.next()){
gr.setWorkflow(false);
gr.deleteRecord();
gs.print('Records Deleted: '+ deleteCount);
}

 }

vkachineni
Mega Sage

First check the record count 

 

removechecksjan();

function removechecksjan()

{

var deleteCount = 0;
var gr = new GlideRecord("u_check_drawer");
gr.addEncodedQuery('sys_created_on=2019-04-18');
gr.query();

var rec_count = gr.getRowCount();

gs.print('Records to be deleted :  '+ rec_count );

while(gr.next())

{
gr.setWorkflow(false);
deleteCount++;


}
gs.print('Records Deleted: '+ deleteCount);
}

BrianD502676804
Kilo Sage

Hi nico,

Try using the table filter in List view to generate your query statement.  The field sys_created_on is a GlideDateTime field, so I do not think it would be equal to a simple date.  Using the table filter gives this result for your query statement:

sys_created_onON2019-04-18@javascript:gs.dateGenerate('2019-04-18','start')@javascript:gs.dateGenerate('2019-04-18','end')

 

Once you build the filter on a list, just right-click on the filter breadcrumbs to copy the underlying query statement.  Give that a try.

Thanks,

-Brian

A-N
Tera Expert

I think problem is with your date check in the EncodedQuery

Try this out....

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);
}