Get Message from GlideRecord.insert when not inserting

Bruno Leclerc
Kilo Expert

Hi,

Let's say, I want to insert a new record with GlideRecord.insert in background script.

If object is inserted, the return value is a new object. OK.

If object is Not Inserted for any reason, the return value is null, and a message is displayed on background console. OK.

I would like to programmatically catch this error to display it or do something with.

Do you know how to do that ?

thanks.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Bruno Leclerc 

you can use this function to get the error message from the gliderecord object of that table

Consider you are trying to insert some record and some BR is aborting the insert with some error message

Using this you can print that error message

gr.getLastErrorMessage();

OR

You can also use Try Catch block to print the exception message

try{
	var gr = new GlideRecord("table");
	gr.initialize();
	gr.field1 = 'hello';
	gr.insert();
}
catch(ex){
	gs.info('Exception' + ex);
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Daniel Gurney1
Tera Expert

 

 

var gr = new GlideRecord('x_123456_tablename');
gr.setValue( 'test_id', '123' );
if ( !gr.insert() ) {
  throw new Error( gr.getLastErrorMessage() );
}

 

 

This will show an error message.  I did not find it very helpful, however it gives you something to throw:

Error during insert of x_123456_tablename ( 123 )

 

Here are some potential reasons why the record did not insert:

  • A field/column on the "x_123456_tablename" table has the "unique" property and the value being inserted is not unique (open the table definition, show the "unique" property in the table columns view)
  • An index on the "x_123456_tablename" table has the "unique" property and the value being inserted is not unique  (see sys_index.list, show the "unique" property in the view)

Other helpful folks have stated that:

  • The value being entered is greater than the size of the field (value length is 40, field size is 32) -- NOTE: I have not seen an error when this happens.  From what I have seen, the data is clipped by ServiceNow without error.
  • There is no space left in the database or on-disk.  -- NOTE: OK, maybe.  This would be the last thing to check.