Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Deleting all the record in incident table even added the limit

Sujata Lenka
Tera Contributor

I am using a code in back ground script to delete 5 p1 record , but it deleting all the incident records in incident table. 

 

script is as below, please help in it.

var gr = new GlideRecord('incident');
gr.addQuery('priority','1');
gr.orderBy('sys_created_on');
gr.setLimit('5');
gr.query();
while(gr.next())
{

gr.deleteMultiple()
}

1 ACCEPTED SOLUTION

Amitoj Wadhera
Kilo Sage

Hello @Sujata Lenka ,

 

 

Please try the below script:

 

 

var gr = new GlideRecord('incident');
gr.addQuery('priority','1');
gr.orderBy('sys_created_on');
gr.setLimit(5);
gr.query();
while(gr.next())
{
gr.deleteRecord();
}

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Regards,

Amitoj

View solution in original post

5 REPLIES 5

Sujata Lenka
Tera Contributor

Can any one please help on this.

Amitoj Wadhera
Kilo Sage

Hello @Sujata Lenka ,

 

 

Please try the below script:

 

 

var gr = new GlideRecord('incident');
gr.addQuery('priority','1');
gr.orderBy('sys_created_on');
gr.setLimit(5);
gr.query();
while(gr.next())
{
gr.deleteRecord();
}

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Regards,

Amitoj

Sandeep Rajput
Tera Patron
Tera Patron

@Sujata Lenka Please update your code as follows.

 

var gr = new GlideRecord('incident');
gr.addQuery('priority','1');
gr.orderBy('sys_created_on');
gr.setLimit(5);
gr.query();
gr.deleteMultiple();

 

 

Why was your script not working.

1. setLimit only accepts number value and you provided a string in argument.

2. deleteMultiple in while loop: - Delete multiple doesn't require a loop

 

Hope this helps

swathisarang98
Giga Sage

Hi @Sujata Lenka ,

 

There is semicolon missing after gr.deleteMultiple();

try with below code :

 

var gr = new GlideRecord('incident');
gr.addQuery('priority','1');
gr.orderBy('sys_created_on');
gr.setLimit('5');
gr.query();
while(gr.next())
{
gr.deleteMultiple(); // semicolon was missing 
}

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang