How to convert string field value to date field using transform map

Tribid2
Kilo Expert

How to convert string field value to date field using transform map

string field value : 01-APR-1967 (u_actual_date)-->string field

date field format : MM-dd-yyyy (actual_start)--> date field

below code not working:

OnBefore

if (!source.u_actual_date.nil()) {
var sec = Date.parse(source.u_actual_date);
var gdt = new GlideDateTime();
gdt.setNumericValue(sec);
target.actual_start = gdt.getDisplayValue();
}

 

1 ACCEPTED SOLUTION

Tribid2
Kilo Expert

I have found the solution by modifying the transform map script

On before

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

 

var gd = new GlideDate();
gd.setDisplayValue(source.u_actual_date, "dd-MMM-yyyy");
var dt = gd.getByFormat("MM-dd-yyyy");
target.actual_date= dt;
//gs.info("SUCCESSFUL : ", dt);

 

})(source, map, log, target);

 

View solution in original post

5 REPLIES 5

Allen Andreas
Administrator
Administrator

Hi,

Once you use the field mapping section and add a new entry here, I believe the transform map will ask you to specify the date format for the field in the import table so that it can map it correct to the target date field you've chosen. It's assumed it would most likely be in string anyway, but it'll ask you for the format like:

dd-MMM-yyyy

and then it'll automatically convert it now that it knows how your imported date format is and appropriately match it to the format set within your instance for the target.

No onBefore script should be needed...have you tried to address this from a field mapping perspective?

Example:

find_real_file.png

So you've pick the correct source field, but the date format field pops up here on the left when you select a target field on the right that is a date field.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Tribid2
Kilo Expert

Hi Allen, 

I have tried the mapping too in the transform map, you have stated above. Unfortunately not working.

Below Error:

Unable to format 31-JUL-2017 using format string MM-dd-yyyy hh:mm:ss for the field actual_start

Hi,

Does your source field really have hh:mm:ss?

Please see my exact screenshot for assistance. I tried to help by giving a pretty good example for you to reference.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Tribid2
Kilo Expert

I have found the solution by modifying the transform map script

On before

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

 

var gd = new GlideDate();
gd.setDisplayValue(source.u_actual_date, "dd-MMM-yyyy");
var dt = gd.getByFormat("MM-dd-yyyy");
target.actual_date= dt;
//gs.info("SUCCESSFUL : ", dt);

 

})(source, map, log, target);