Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Need some help with a FormCalc script...

Avatar

Level 4

I have a form that autofills some text fields based on a date field. It works great, but when I open the form, the fields are already populated with a default date, when I actually set the "Week Starting" date, it changes to what I want, I just dont want the fields to have anything in them when the form is opened.  The FormCalc script I'm using is as follows

var dateNum = date2num(form1.Checklist.weekStart.formattedValue,"MMMM DD, YYYY")

$.rawValue = num2date(dateNum,"MM/DD")

Here is a link to the file itself

https://db.tt/UScOAGZr


1 Accepted Solution

Avatar

Correct answer by
Level 10

It's FormCalc, that block just checks the calendar dropdown to make sure they select a Monday.

I like to put as much of my code as I can in one spot so it's easier to change things. That's why I suggested doing it all from the calendar dropdown.

So you could do something like:

var selectedDate = Date2Num($,"YYYY-MM-DD")

var dayOfWeek = Num2Date(selectedDate, "E")

if (dayOfWeek <> 2) then

          xfa.host.messageBox("Week Starting must be a Monday")

          $ = null

          xfa.host.setFocus("$")

endif

Table1.Row1.dayMonday = num2date(selectedDate, "MM/DD")

Table1.Row1.dayTuesday = num2date(selectedDate + 1, "MM/DD")

Table1.Row1.dayWednesday = num2date(selectedDate + 2, "MM/DD")

Table1.Row1.dayThursday = num2date(selectedDate + 3, "MM/DD")

Table1.Row1.dayFriday = num2date(selectedDate + 4, "MM/DD")

Table1.Row1.daySaturday = num2date(selectedDate + 5, "MM/DD")

Table1.Row1.daySunday = num2date(selectedDate + 6, "MM/DD")

(I ungrouped the table first as it was causing the need to use xfa.resolvenodes.)

View solution in original post

8 Replies

Avatar

Level 10

You need to wrap the statement in an if statement to test if the date field has a value.

if (weekStart <> null) then

$.rawValue = num2date(dateNum,"MM/DD")

endif

This will show a zero in the fields, to get rid of that set their display patterns to "Allow Zero" to rid of that.

Personally I'd put all the code on the date field and populate the other fields from there instead of using the Calculate event of the fields themselves.

Also your changing the date field to readOnly after date selection may not be that great of a user experience, if they pick the wrong date they'll have to re-open the form. You could put a check in for making sure they select a Monday and I'd move the code to the Exit event of the date field:

var selectedDate = Date2Num($,"YYYY-MM-DD")

var dayOfWeek = Num2Date(selectedDate, "E")

if (dayOfWeek <> 2) then

          xfa.host.messageBox("Week Starting must be a Monday")

          $ = null

          xfa.host.setFocus("$")

endif

Avatar

Level 4

So your saying it would be best to use the second batch of code:


on the Exit event of the weekStart and then have the days of the week be dayOfWeek[0], dayOfWeek[1], and so on? This is FormCalc correct?

UPDATE: I renamed weekStart to selectedDate, I put the code above in the Exit event of the selectedDate, removed the code I had on the week days, changed them all to be dayOfWeek, preview it, select a Monday, it pops up and says "Week Starting must be Monday", I go back do design view and check everything, looks good, I preview it again, the warning pops up again and LiveCycle locks up. If I use my code and shange my 2nd statement to match the first part of your message, all the fields have )') in them before I select a date.

Avatar

Correct answer by
Level 10

It's FormCalc, that block just checks the calendar dropdown to make sure they select a Monday.

I like to put as much of my code as I can in one spot so it's easier to change things. That's why I suggested doing it all from the calendar dropdown.

So you could do something like:

var selectedDate = Date2Num($,"YYYY-MM-DD")

var dayOfWeek = Num2Date(selectedDate, "E")

if (dayOfWeek <> 2) then

          xfa.host.messageBox("Week Starting must be a Monday")

          $ = null

          xfa.host.setFocus("$")

endif

Table1.Row1.dayMonday = num2date(selectedDate, "MM/DD")

Table1.Row1.dayTuesday = num2date(selectedDate + 1, "MM/DD")

Table1.Row1.dayWednesday = num2date(selectedDate + 2, "MM/DD")

Table1.Row1.dayThursday = num2date(selectedDate + 3, "MM/DD")

Table1.Row1.dayFriday = num2date(selectedDate + 4, "MM/DD")

Table1.Row1.daySaturday = num2date(selectedDate + 5, "MM/DD")

Table1.Row1.daySunday = num2date(selectedDate + 6, "MM/DD")

(I ungrouped the table first as it was causing the need to use xfa.resolvenodes.)

Avatar

Level 4

Well this is strange, when I preview it in LiveCycle, it works fine, but after I save it and open it in Acrobat Pro, the fields don't get populated when I select a date.

Avatar

Level 10

Works fine for me...did you remove all the old code?

Avatar

Level 4

OK, what was happening was I had an action that disabled editing of the weekStart date once a date is entered, this was at the request of the IT manager, so I need to add that into the script. Would I just add selectedDate.access = readOnly before the endIf?

Avatar

Level 10

No you need to reference the field itself, selectedDate is just a variable, so after all the code above you'd put:

$.access = "readOnly".

As I said above though, not a great user experience if they pick the wrong date, but the check for choosing a Monday should help.