What error caused deleteRecord to fail

CoyoteRay1
Tera Contributor

Good Day,

Forgive me if this was already answered, I am new to a ServiceNow development role.

I have a query that runs successfully in a scoped application and it returns the correct number of records.  A call to deleteRecord fails (returns false) however there is no error message captured.  Is there a place to capture why the delete failed?  My account has access to delete from the web UI. 

 

Below is the code for reference: 

gs.info("Started");
var gr = new GlideRecord('discovery_range_item');
gr.addEncodedQuery('nameSTARTSWITH15.15.15.15');
gr.query();

gs.info("Pulled " +gr.getRowCount() + " records.");
gr.next();
var rtn = gr.deleteRecord();
gs.info("Return from deleteRecord: " +rtn);

var errormessage = gr.getLastErrorMessage(); 

gs.info("Error message: " +errormessage); 
gs.info("Finished:");

 

Log reads:

Started
Pulled 1 records
Returned from deleteRecord: false
Error message: null
Finished
9 REPLIES 9

Bryan Tay3
Mega Guru

hi there, 

have you try look at System Logs -> System Log -> Errors/All?

hope this helps.

Thank you, will get with the internal ServiceNow team to look at those - I don't have access 😞

 

Is there a way to capture the error message within the script?  If so, can you point me in the right direction?

We could not find any errors or logs as to why the deleteRecord failed.  When a colleague runs the script the record is removed as expected.  My ServiceNow Administrator is looking to why this is failing, will reply once we find the cause.

shloke04
Kilo Patron

Hi,

According to your query, I don't see where you are iterating the loop after deleting your First Record. You are getting the count correct initially but after that once the First Record is deleted it needs to be iterated to go to the Second Record based on your Encoded Query for Deletion. Please use the below Script which should work for you:

Script:

var gr = new GlideRecord('discovery_range_item');

gr.addEncodedQuery('nameSTARTSWITH15.15.15.15');
gr.query();

while(gr.next()){

gr.deleteRecord();

}

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke
Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke