unable to format 07/2/2020 format string error message.

OluseyiS3924078
Tera Contributor

I am getting an error when I run my Transform script.    
Error message - Unable to format 07/20/2020 to format string using yyyy-mm-dd for field u_purchase date.

The date format in u_purchase date is mm/dd/yyyy. I set the date format for the field mapping to match the source field - u_purchase_date. Still geting the same error message in the Transform logs.  Any suggestions on what i can do to resolve this? 

1 REPLY 1

vaishali231
Tera Guru

hey @OluseyiS3924078 

 

The issue is due to incorrect date format tokens. In ServiceNow, date formats are case-sensitive:

MM = Month

mm = Minutes

If you use yyyy-mm-dd, the system reads it as year-minute-day, which causes the formatting error.

Since your source format is MM/dd/yyyy, you can handle it safely with a transform script like below.

 onBefore Transform Script:

(function transformRow(source, target, map, log, isUpdate) {

if (source.u_purchase_date) {

try {
var sourceDate = source.u_purchase_date.toString().trim();

var gdt = new GlideDateTime();
gdt.setDisplayValue(sourceDate, "MM/dd/yyyy"); 

target.u_purchase_date = gdt;

} catch (e) {
log.error("Date conversion failed for value: " + source.u_purchase_date + " Error: " + e.message);
}

}

})(source, target, map, log, action === "update");

*************************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

 

Regards

Vaishali Singh