javascript populate fields based on date
Copy link to clipboard
Copied
In my AcroForm
I have a date field (DateOfInsp)
I have a Fiscal Year field (FY) and a Quarter field (Qtr)
I want to populate the FY (yyyy) field and the Qtr field (1,2,3,4) based on the date field
The fiscal year for my organization starts 10-1 and ends 9-30 of the following year
I am looking for the javascript to do this, or guidance so I can learn myself...I am pretty new and just want to automate my forms as much as possible and provide data validation whenever possible
I have seen many posts related to this, but haven't figured it out yet...
Any help is greatly appreciated
Copy link to clipboard
Copied
What are the start and end dates of each quarter? So if the date is before September 30th it's considered to be in the fiscal year that is one before the calendar year? For example, 09-01-2020 is in Q4 of 2019?
Copy link to clipboard
Copied
In your example above...09-01-2020 is Q4 of FY2020
So
10-01-2020 thru 12-31-2020 Q1 FY2021
01-01-2021 thru 03-31-2021 Q2 FY2021
04-01-2021 thru 06-30-2021 Q3 FY2021
07-01-2021 thru 09-30-2021 Q4 FY2021
Copy link to clipboard
Copied
OK, then you can use this code as the custom calculation script of the "Qtr" field:
var dateOfInspString = this.getField("DateOfInsp").valueAsString;
if (dateOfInspString=="") {
event.value = "";
this.getField("FY").value = "";
} else {
var dateOfInsp = util.scand("mm-dd-yyyy", dateOfInspString);
if (dateOfInsp.getMonth()>=9) {
event.value = "4";
this.getField("FY").value = dateOfInsp.getFullYear()+1;
} else {
if (dateOfInsp.getMonth()>=6) event.value = "3";
else if (dateOfInsp.getMonth()>=3) event.value = "2";
else event.value = "1";
this.getField("FY").value = dateOfInsp.getFullYear();
}
}
Copy link to clipboard
Copied
Thanks so much...however the quarter designations were wrong...
Through some trial and error I changed the script as follows:
var dateOfInspString = this.getField("DateOfInsp").valueAsString;
if (dateOfInspString=="") {
event.value = "";
this.getField("FY").value = "";
} else {
var dateOfInsp = util.scand("mm-dd-yyyy", dateOfInspString);
if (dateOfInsp.getMonth()>=9) {
event.value = "1";
this.getField("FY").value = dateOfInsp.getFullYear()+1;
} else {
if (dateOfInsp.getMonth()>=6) event.value = "4";
else if (dateOfInsp.getMonth()>=3) event.value = "3";
else event.value = "2";
this.getField("FY").value = dateOfInsp.getFullYear();
}
}
Now they are correct and the Quarter field populating is working as desired...Also, if it wasn't clear I am trying to have the FY (yyyy) populate in the FY field in addition to the quarter being populated, so I don't understand the "this.get("FY").value" part of the script...does the script need this?...Since the FY field would not be user entered, but is desired to be populated after the date is picked...I would imagine I would need a custom script for the FY field, where the date range for the FY field is 10-01-yyyy thru 09-30-yyyy of the following year...
Thanks!
Copy link to clipboard
Copied
I used the exact dates you provided... Anyway, glad to hear it's working.
This script will populate both fields, Qtr and FY. You don't need to create a separate script for the latter.
Copy link to clipboard
Copied
Thanks...I see, now that I had changed the FY field name in the document after posting the question...so that is why the FY (now named FiscYR) isn't populating...I will make the edits and see what happens...
Slowly, the more I get into doing these kinds of scripts, the more I learn what the scripts mean and how they are put together...
Thanks...will let you know what happens
Copy link to clipboard
Copied
With the edit to the object name...It is working now as desired...Thanks again...I spent a whole afternoon the other day trying to find this solution on Google and just couldn't find something that matched...

