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

Prasad Pagar
Mega Sage

Hi Bruno,

Can you please show us image which error you are talking about after background script failure?

Thank you
Prasad

Bruno Leclerc
Kilo Expert

I know I have an error with encryption. Message is :

Background message, type:error, message: Invalid attempt to insert non-encrypted data into field: u_xxxxx in table: cmdb_ci_appl.

But my question is : how can I catch this message programmatically to do something with ....

Thanks.

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

Bruno Leclerc
Kilo Expert

Thanks Ankur,  gr.getLastErrorMessage() do the job for me.