• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Javascript code that sometimes works, and sometimes doesn't

Explorer ,
Mar 04, 2019 Mar 04, 2019

Copy link to clipboard

Copied

Hello,

I'm hoping someone can help me figure out why certain code sometimes works, and sometimes doesn't. I didn't write any of the code, I copied and pasted the parts I needed from different google searches (Thank you, internet!).

The pdf contains 12 months. Each month has 5 boxes (representing weeks) with dates in this format "mmm d, yyyy". The idea is that if I enter a date for the first box in the first week of January, all the other dates are auto populated by adding 7 days to the previous box. The reason for the 5 boxes is because sometimes you get 5 7 day periods in one month. For example, if the first box is Jan 1, then the following boxes will populate Jan 8, Jan 15, Jan 22, Jan 29. 5 Boxes.  However, If, instead, the first box is Jan 4, then the other boxes fill be Jan 11, Jan 18, Jan 25. The fifth box would technically be February so the 5th box goes blank. In that case, the first box in February would be Feb 1st. SO forth and so on.

To make all this happen, there is a document level script:

function DateAddDays(cFormat, cDate, nDays) {

// convert date string to date object

var cReturn = "";

if(cDate.toString() != "") {

var oDate = util.scand(cFormat, cDate);

// add nDays

oDate.setDate(oDate.getDate() + nDays);

cReturn = util.printd(cFormat, oDate);

}

// return formatted date date stirng

return cReturn;

} // end AddDays funciton

Then, there is a script in the first box of each month (Except January), here's the one for February:

// custom calculation script for date + 7 days field

// field names for base date

var cDate='Ene7';

if (this.getField(cDate).valueAsString=="") {cDate = 'Ene6'; }

// format for date fields

var cDateFormat = "mmm d, yyyy";

// number of days to add

var nNumDays = 7;

// add seven days

event.value = DateAddDays(cDateFormat, this.getField(cDate).valueAsString, nNumDays);

There's a script in the 5th box of every month:

// custom calculation script for date + 7 days field

// field names for base date

var cDate = 'Feb6';

// format for date fields

var cDateFormat = "mmm d, yyyy";

// number of days to add

var nNumDays = 7;

// add seven days

x = DateAddDays(cDateFormat, this.getField(cDate).valueAsString, nNumDays);

if (x[0]=='F') {

event.value = x;}

else {event.value = ""; r=false}

Finally, there's a script in all the other box fields that just adds 7 days to the previous:

// custom calculation script for date + 7 days field

// field names for base date

var cDate = 'Feb3';

// format for date fields

var cDateFormat = "mmm d, yyyy";

// number of days to add

var nNumDays = 7;

// add seven days

event.value = DateAddDays(cDateFormat, this.getField(cDate).valueAsString, nNumDays);

The problem I'm having is that even though I copied and pasted the same scripts for each month and just changed the field names, it seems only January through February work consistently. IN other months, sometimes it adds 5 days instead of 7, or for some reason, April's 5th box never displays a date, even though it should. All you have to do to reproduce is change the date on January 1st, and look at the dates in April and you'll see the 5th box never shows a date, and the first box in May will be wrong.

Do any of you think you can determine why it is inconsistent? I'm attaching the form for reference, if it helps.

Thanks in advance!

Link (Couldn't figure out how to attach): http://https://www.dropbox.com/s/vm7scij2hpvkutd/S-3-S_4up%20Auto%20%281%29.pdf?dl=0

TOPICS
Acrobat SDK and JavaScript , Windows

Views

479

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 04, 2019 Mar 04, 2019

Your link is broken, but I got the file and had a look at it.

The issue, I believe, is a classic field calculation order. If you're using the values of a previous field (Y) to calculate the value of field X, so that means that Y must be calculated before X. If it's not, X will use an incorrect value (the one Y had before) and the results will be incorrect.

For example, Feb7 relies on the value of Feb6, but it is calculated before it, as can be seen from the fields calculation order list:

The same a

...

Likes

Translate

Translate
Community Expert ,
Mar 04, 2019 Mar 04, 2019

Copy link to clipboard

Copied

Your link is broken, but I got the file and had a look at it.

The issue, I believe, is a classic field calculation order. If you're using the values of a previous field (Y) to calculate the value of field X, so that means that Y must be calculated before X. If it's not, X will use an incorrect value (the one Y had before) and the results will be incorrect.

For example, Feb7 relies on the value of Feb6, but it is calculated before it, as can be seen from the fields calculation order list:

The same applies to many other fields in your file.

In addition, I think the way you're doing these calculations is not very good. Instead of this "daisy chain" just use the first value ("Ene3") to calculate everything else in a single function. That will make your work much easier...

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 04, 2019 Mar 04, 2019

Copy link to clipboard

Copied

I think you’re right, the problem is the order. How can I re-write it so the order of the calculation isn’t backward?

Also I’ll try to eliminate the Daisy chain style.

AR

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2019 Mar 04, 2019

Copy link to clipboard

Copied

This is not something you fix in the code, but in the dialog I took a screenshot of.
However, if you change your code to the way I described it will no longer be an issue.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 04, 2019 Mar 04, 2019

Copy link to clipboard

Copied

try67 you so much! I hadn't looked at the screenshot because I just saw the reply in the email. Did what you said, works great! Thanks!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 04, 2019 Mar 04, 2019

Copy link to clipboard

Copied

Is there a way to turn those dates to Spanish?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2019 Mar 04, 2019

Copy link to clipboard

Copied

Yes, like this:

util.printd("date(es){MMM D, YYYY}", new Date(), true)

Notice the format has to be in upper-case for this to work.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 04, 2019 Mar 04, 2019

Copy link to clipboard

Copied

Great! I will try this tonight   , I’m assuming I just add that to the end...

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 04, 2019 Mar 04, 2019

Copy link to clipboard

Copied

I tried adding that line at the end of my current code but it didn't do anything, where should i add that?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2019 Mar 04, 2019

Copy link to clipboard

Copied

Instead of your current util.printd command, with the correct parameters.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 05, 2019 Mar 05, 2019

Copy link to clipboard

Copied

Ok, clearly I'm far worse at this than I thought I was.

I've replaced the current util.printd command, so the script currently looks like this:

function DateAddDays(cFormat, cDate, nDays) {

// convert date string to date object

var cReturn = "";

if(cDate.toString() != "") {

var oDate = util.scand(cFormat, cDate);

// add nDays

oDate.setDate(oDate.getDate() + nDays);

cReturn = util.printd("date(es){MMM D, YYYY}", new Date(), true);

}

// return formatted date date stirng

return cReturn;

} // end AddDays funciton

However it turned ALL dates in ALL fields into mar 5, 2019. Then I changed the format in each box to MMM D, YYYY. This produces the following result: "D, YYYY" as the final value in the field. It does not recognaize the command in caps.

Where did I go wrong?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2019 Mar 05, 2019

Copy link to clipboard

Copied

Change:

cReturn = util.printd("date(es){MMM D, YYYY}", new Date(), true);

To:

cReturn = util.printd("date(es){MMM D, YYYY}", oDate, true);

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 05, 2019 Mar 05, 2019

Copy link to clipboard

Copied

It's so strange... This worked for most field boxes but it did not work for any of the January boxes, and a few random ones between February and December. Additionally, it somehow changed the calculation in the fifth boxes, they're no longer working. Could it be because I haven't finished fixing the daisy chain style calculations yet?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2019 Mar 05, 2019

Copy link to clipboard

Copied

Yes. Unless the calculation order is fixed it won't work correctly.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 05, 2019 Mar 05, 2019

Copy link to clipboard

Copied

The calculation order is already corrected, what I haven't finished yet is calculating everything based off of the first value ("Ene3"), instead, most fields are still calculated based on the value of the previous field. Do you think that's the problem?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2019 Mar 05, 2019

Copy link to clipboard

Copied

Could be. Hard to say without seeing the actual file.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 05, 2019 Mar 05, 2019

Copy link to clipboard

Copied

LATEST

ok, I will give that a try. Thanks for all your patience!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines