Script Creating Change Record but then Record is not Found

fcaruso123
Tera Expert

I have a scripted web service used to create a change record. The script pulls values from JSON object and assigns to individual fields:

 

var grCHG = new GlideRecord('change_request');
grCHG.initialize();
grCHG.risk = dataJSON.risk;
grCHG.start_date = dataJSON.start_date;
grCHG.end_date = dataJSON.end_date;
grCHG.insert();

 

This is working correctly except when one of the date fields is sent with a bad date format. The insert() is running successfully, I am getting a change number returned, but when I go look for that change number it does not exist. No errors are getting thrown. I can do a GlideRecord lookup of the change number immediately after the insert() and it finds the change record. But when I go into the platform the change is record is not found. If I remove the dates from the insert the change is created and I can locate it in the platform. I looked in deleted records and its not there.
 
How does the change get created successfully but then cannot be found?
 
Thank you
Frank
 
 
 
 
5 REPLIES 5

Collin Romeijn
Kilo Guru

HI Frank,

when you use the "create new or Initialize" a temporary form/record with a number will be reserved for the creation. But if for some reason an ABORT (most likely a businessrule) prevents it from creating after an insert / submit... the temporary record will be dismissed. Because there is no "real" record created on the table there is nothing to delete... the action is simply aborted.

In the script you could always use the Try / catch method to get some error handling.
try...catch - JavaScript | MDN

Hopefully this gives you some guidons.
Kind Regards,

Collin

I did try adding a try/catch but it is not failing. Is there somewhere else I can look to see what might be causing the record to be deleted?

try {
            grCHG.insert();
        } catch (ex) {
            var message = ex.message;
            gs.error("Error in system logs: " + message);
			return;
        }

Ankur Bawiskar
Tera Patron
Tera Patron

@fcaruso123 

you need to validate the date format before actually setting those field values

Finalize a date format with your 3rd party team and validate

If validation passes then set the field value

var startDateIsValid = false;

// use regex logic to check if start date is valid
// if yes then set the flag as true

var endDateIsValid = false;

// use regex logic to check if end date is valid
// if yes then set the flag as true

var grCHG = new GlideRecord('change_request');
grCHG.initialize();
grCHG.risk = dataJSON.risk;
if (startDateIsValid)
    grCHG.start_date = dataJSON.start_date;
if (endDateIsValid)
    grCHG.end_date = dataJSON.end_date;
grCHG.insert();

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@fcaruso123 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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