Best Practice

  • Better error handling with exception objects

    Written on

    under

    If you’ve spent time writing ServiceNow Script Includes, you’ve probably run into the classic issue of checking for errors when working with GlideRecord. Normally, you’d write code like this:

    var gr = new GlideRecord('incident');
    gr.short_description = 'Test Incident';
    if (!gr.insert()) {
        gs.error('Failed to insert: ' + gr.getLastErrorMessage());
    }

    While this works, it’s easy to miss error handling, especially in larger scripts, and the logic for checking success/failure can get repetitive.

    (more…)