Skip to main content
Participating Frequently
March 1, 2018
Answered

JavaScript Help Needed

  • March 1, 2018
  • 1 reply
  • 1495 views

Hello,

I have been working on designing a PDF where the first box auto generates the dates month in the format DD/YYYY, and the subsequent fields calculate in the same format, descending by 1 month increments from the date in the first field. The first box I have called Today, the subsequent boxes are listed as Today_1, Today_2, Today_3, etc. I.E.

02/2018     01/2018    12/2017    11/2017    10/2017

^This needs to auto generate to the current month, and then proceed to go back a month at a time for 105 boxes.

I thought I had this working, but I am getting a lot of errors now stating that..

InvalidSetError: Set not possible, invalid or unknown.

Field.value:1:Field Today:Format

The JavaScript causing this issue I am sure is one I have set as a Document Javascript which is:

function getPastDate(Today, months){

var cDate = util.printd("mm/yyyy",new Date(this.getField(Today).value));

var sDate = util.scand("mm/yyyy", cDate);

          if ( sDate== null ){

                    app.alert("Please enter a valid date of the form \"mm/yyyy\".")

          }else {

          var pDate = new Date(sDate.getFullYear(),(sDate.getMonth() + months),sDate.getDate());

          return util.printd("mm/yyyy",pDate);

          }

}

In Today_1 in the Custom Calculate script, the script I am using to go back in 1 month increments is:

event.value = getPastDate("Today",-1);

Hoping to get some help I desperately need!

Thank you!

This topic has been closed for replies.
Correct answer try67

Here is the link to my Document on the Google Drive. Thank you for looking at this for me!

Equifax TU Extra Data 1.pdf - Google Drive


OK, I see the issue now. The problem is that you can't define a Date object based on just a month and a year, because that doesn't define an actual date... So the solution is to artificially insert a date, like 01. Also, the code is quite cumbersome and has all kinds of unnecessary things. So use this code, instead:

function getPastDate(Today, months){

    var v = this.getField(Today).valueAsString

    if (v=="") return "";

    var cDate = util.scand("dd/mm/yyyy",new Date("01/"+v));

    if (cDate== null){

        app.alert("Please enter a valid date of the form \"mm/yyyy\".")

        return "";

    } else {

        cDate.setMonth(cDate.getMonth() + months);

        return util.printd("mm/yyyy",cDate);

    }

}

1 reply

try67
Community Expert
Community Expert
March 1, 2018

You have to return a value from the function, even if there's an error. In that case you can return an empty string or something.

ShaylaRAuthor
Participating Frequently
March 1, 2018

I switched the script to:

function getPastDate(Today, months){

var cDate = util.printd("mm/yyyy",new Date(this.getField(Today).value));

var sDate = util.scand("mm/yyyy", cDate);

          if ( sDate== null ){

                    app.alert()

          }else {

          var pDate = new Date(sDate.getFullYear(),(sDate.getMonth() + months),sDate.getDate());

          return util.printd("mm/yyyy",pDate);

          }

}

It appears to have stopped the pop up of the previous error and the debugger isn't showing anything in it. So I think your advice solved my issue! I am going to be attempting to self-teach myself Javascript in the future, as this is very new for me. Wish me luck.

try67
Community Expert
Community Expert
March 1, 2018

That does not solve the issue I mentioned...

Add this command to the end of the function:

return "";