toDate() errors

toDate() errors

I have a form called Lab_Test_Value, and two form fields that are doing a cascading lookup, as follows:

1) Field_1 is a 'Lookup' field, where the user picks a value from a drop-down list.

During the 'On User Input' event, the key value selected in Field_1 is used to lookup and generate a distinct list of string date values. The format for the resulting date list "yyyy-MM-dd". This list of string date values is used to populate the options for Field_2. Here is the Field_1 'On User Input' event code, which works fine:

dat_patient_id = input.Patient_Profile;
if (dat_patient_id  !=  null)
{
    patient_lt  =  Lab_Test_Value [Patient == dat_patient_id];
    patient_lt_dates = patient_lt.distinct(Date_field);
    Field_2:ui.add(patient_lt_dates);
}

2) Field_2 is a 'Drop Down' field, where the user picks a single string date value from the drop-down list of "yyyy-MM-dd" string date values which was generated from Field_1.

During the Field_2 'On User Input' event, the string date value selected in Field_2 is parsed and reassembled so that it matches the date format in the Application Settings ("MM-dd-yyyy"). Once reassembled, I am trying to transform that date string into a Date using toDate(). I need to place the resulting Date value into a Field_3.

I am unable to do this, as I keep getting a script error on the line using the toDate() transform.

Here is the Field_2 'On User Input' event code:

field2_str1 = input.Field_2;
string_length = field2_str1.length();
field2_year = field2_str1.subString(0,4);
field2_mo = field2_str1.subString(5,7);
field2_day = field2_str1.subString(8,string_length);
//assemble date into "MM-dd-yyyy":
field2_str2 = trim(field2_mo + "-" + field2_day + "-" + field2_year);
//both of the next two lines fail with the same error...
//field2_date = dateValue(field2_str2); <---fail
field2_date = field2_str2.toDate();
<---fail

input.Field_3 = field2_date;


The above code fails with this error:

Error is executing Set Variable task. Unable to update template variable field2_date.
---Error evaluating DATETIME expression:


Any idea why this date string 'field2_str2' is not able to be transformed into a date?

Thanks in advance!

Karl Forsyth