How does getLastErrorMessage work?

There is a method on the GlideRecord object called getLastErrorMessage(). This method, according to the docs (opens in a new tab), returns the last error message that was generated by a database operation.

The docs don't go into much detail, so here's some more information on how it works.

Assume we've set up a business rule which uses setAbortAction(true) to prevent a database insert whenever we try to set the number field to the boolean value of false on a new or existing incident record.

var grIncident = new GlideRecord('incident');
grIncident.setValue('number', false);
var incidentId = grIncident.insert();;
// *** Output: Operation against file 'incident' was aborted by Business Rule 'Test^57d28f7adb714650553e8f8d
// getLastErrorMessage() isn't available on the table level, however.
var grIncident2 = new GlideRecord('incident');'grIncident2: ' + grIncident2.getLastErrorMessage());
// *** Output: null
// getLastErrorMessage() is also only availble in the context in which the error is thrown. Trying to access it from a different context, using the same sys_id doesn't work.
var grIncident3 = new GlideRecord('incident');
grIncident3.get(incidentId);'grIncident3: ' + grIncident3.getLastErrorMessage());
// *** Output: null