Skip to main content
Participating Frequently
Mar 1, 2018 12:00 AM UTC
Answered

JavaScript Help Needed

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 is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Mar 1, 2018 4:44 PM UTC

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);

    }

}

13 replies

try67
Community Expert
Community Expert
Mar 1, 2018 9:35 AM UTC

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.

Translate
ShaylaRAuthor
Participating Frequently
Mar 1, 2018 3:20 PM UTC

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.

Translate
try67
Community Expert
Community Expert
Mar 1, 2018 3:27 PM UTC

That does not solve the issue I mentioned...

Add this command to the end of the function:

return "";

Translate