insert failure is not caught in try - catch script

Eli Guttman
Tera Guru

Hi,

I have a script where i'm performing insert to a certain table. i wrapped that script with try - catch statement.

I noticed that in case i try to insert, and there is a error due to Unique Key violation - they script does not got to the catch statement.

Would like to get some advise on how to handle such case. 

1 ACCEPTED SOLUTION

Exactly.

The return value is described as: "Unique ID of the inserted record, or null if the record is not inserted."

var id = '11111111111111111111';
var gr = new GlideRecord('my_table');
gr.initialize();
gr.setValue('id',id);
if (gr.insert() == null)
{
  gs.info('Gosh! That failed :(');
}
else
{
  gs.info('Yeehaa! That worked :D');
}

View solution in original post

8 REPLIES 8

Carlos Camacho
Mega Sage
Mega Sage

Hi,

Please share your code so the community can help you.

Use the button "Insert/edit code sample" of the editor to format it in a way that is easy to read.

SaschaWildgrube
ServiceNow Employee
ServiceNow Employee

Check the return code of the insert() function!

Throwing exceptions is evil anyway - says Google. 

you mean check if the return value is null?

Exactly.

The return value is described as: "Unique ID of the inserted record, or null if the record is not inserted."

var id = '11111111111111111111';
var gr = new GlideRecord('my_table');
gr.initialize();
gr.setValue('id',id);
if (gr.insert() == null)
{
  gs.info('Gosh! That failed :(');
}
else
{
  gs.info('Yeehaa! That worked :D');
}