- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2023 08:39 AM
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:
Error | Unparseable 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:
Information | BEFORE termDate9/21/2023 |
Information | AFTER 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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2023 09:26 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2023 09:05 AM
Hello @Casey23 ,
You need not to write script for parsing the date field, you can directly try to load data in this format '9/21/2023' and ServiceNow date field will adjust the data and update the field OOB.
If this response clears up your doubt, kindly flag it as both helpful and correct.
Thanks,
Alka
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2023 09:10 AM
In the field mapping, the date shows up in the target table correctly. However, in this case I am also creating a request and you can't map directly into a variable. So that's why converting the date is necessary.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2023 09:19 AM
If the variable type is date then you can try mapping without parsing it. It should work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2023 09:26 AM
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);