Cognito-forms – Default Date based on Drop Down selection in Cognito Forms

cognito-forms

I have 3 fields. "Project Type", "Calculated Time" and "Due Date". The "Calculated Time" field is hidden from view ("Show This Field = Never") and set to default to a number that is based on what the user selects in the drop down field named "Project Type". Here is what I've used there:

=ProjectType = "Flyer: Up to 30 Products" ? "7" : ProjectType = "Flyer: Up to 60 Products" ? "12" : ProjectType = "Flyer: 60+ Products" ? "18" : "7"

I would like the "Due Date" field to show a default date based on today's date PLUS the number of days calculated above. (IE: If today is 5/20/15 and i've selected "Flyer: Up to 30 Products" it should add 7 days and the default value would then be 5/27/15. I tried something like below and got the error "No applicable method 'AddDays' exists in type 'DateTime' at character 15"

=DateTime.Today.AddDays(CalculatedTime)

Can someone please help as to why I cannot call upon the "CalculatedTime" field using this method. When I replace CalculatedTime with a number it works fine.

Additional screenshots here:

enter image description here

enter image description here

Best Answer

I am a developer for Cognito Forms.

You are very close to getting the calculation to work, AddDays takes an integer value and the value and what you were generating is a decimal value. You will need to add a little extra code to your Due Date calculation to get it running correctly.

=DateTime.Today.AddDays(Int32(CalculatedTime))

One other change I would make to your calculation in the CalculatedTime field would be to remove the quotation marks (") from around the numebrs, so your calculation looks like this:

=ProjectType = "Flyer: Up to 30 Products" ? 7 : ProjectType = "Flyer: Up to 60 Products" ? 12 : ProjectType = "Flyer: 60+ Products" ? 18 : 7