Why wouldn't my date parse correctly? (Transform script)

Casey23
Tera Guru

I am having an issue trying to parse a date in a transform script. In an IDE, I can run the following code and it works as expected:

 

// Input date in "mm/dd/yyyy" format
var inputDate = "9/21/2023"; // Replace with your date
console.log("Input date:", inputDate);

// Split the input date into components
var dateComponents = inputDate.split('/');

// Ensure the month and day have two digits by padding with '0' if necessary
var month = String(dateComponents[0]).padStart(2, '0');
var day = String(dateComponents[1]).padStart(2, '0');
var year = dateComponents[2];

// Form the converted date in "yyyy-mm-dd" format
var formattedDate = year + '-' + month + '-' + day;

console.log("Converted date:", formattedDate);

 

 The output looks like this:

 

Input date: 9/21/2023
Converted date: 2023-09-21

 

 

In ServiceNow, I have a transform script that is trying to convert that same date. I get an error in the logs that says:

ErrorUnparseable date: "2023-undefined-undefined": java.text.ParseException: Unparseable date: "2023-undefined-undefined": java.base/java.text.DateFormat.parse(DateFormat.java:395)

 

This error aligns with my script level logging as I can see the original date, and then after the attempt at converting, it only seems to pickup the year:

InformationBEFORE termDate9/21/2023

 

InformationAFTER termDate: 2023-undefined-undefined

 

Here is the logic in the transform script for the date:

 

(function runTransformScript(source, map, log, target /* GlideRecord */ ) {

    // Check if the Comparison field contains the letter "D" (aka Termination)
    gs.log('BEFORE empNumber' + source.u_empnumber);
    gs.log('BEFORE jobTitle' + source.u_jobtitle);
    gs.log('BEFORE termDate' + source.u_terminationdate);
    if (source.u_comparison.indexOf('D') !== -1) {

        // Get the values from the source CSV file
        var empNumber = source.u_empnumber;
        var jobTitle = source.u_jobtitle;
        var originalTermDate = source.u_terminationdate;
		var dateComponents = originalTermDate.split('/');
		var month = String(dateComponents[0]).padStart(2, '0');
		var day = String(dateComponents[1]).padStart(2, '0');
		var year = dateComponents[2];

		var termDate = year + '-' + month + '-' + day;
		
		
        gs.log('AFTER empNumber: ' + empNumber);
        gs.log('AFTER jobTitle: ' + jobTitle);
        gs.log('AFTER termDate: ' + termDate);

 

 

Is there something I should be doing differently (or can do differently)? Why would this work in an IDE but not in ServiceNow?

 

1 ACCEPTED SOLUTION

Use the below code for parsing without using .padStart(2,0) then it should work:-

var originalTermDate = '9/22/2023';
		var dateComponents = originalTermDate.split('/');
		var month = String(dateComponents[0]);
		var day = String(dateComponents[1]);
		var year = dateComponents[2];
   		var termDate = year + '-' + month + '-' + day;
        gs.info('AFTER termDate: ' + termDate);

 

View solution in original post

5 REPLIES 5

I'll need to do some additional validation to make sure it handles different dates correctly, but this appears to be the fix.  The request was created with the correct date in the variable , and no errors appear in the logs.

 

Thank you!