- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2022 05:47 AM
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.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
-
Team Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2022 08:47 AM
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');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2022 05:57 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2022 08:14 AM
Check the return code of the insert() function!
Throwing exceptions is evil anyway - says Google.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2022 08:27 AM
you mean check if the return value is null?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2022 08:47 AM
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');
}