Script Creating Change Record but then Record is not Found
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2025 08:02 AM
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();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2025 02:21 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2025 05:54 AM
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;
}
.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2025 06:37 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2025 07:16 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader