Skip to main content
drslrb
Known Participant
February 1, 2025
Answered

PDF script to clear fields

  • February 1, 2025
  • 1 reply
  • 682 views

Hello, I'm having trouble with a PDF document script to reset fields when the document is opened on a different date than the date of the last edit.  I've tried the code below, but it didn't work for me.  Can someone here assist me with this? Thanks

   function clearFormOnNewDate() {
        var today = new Date();
        var lastFilledDate = this.getField("date").value; // Replace "date" with the actual field name in your PDF

        if (lastFilledDate != today.toLocaleDateString()) {
      this.getField("date").value = "";
      this.getField("off").value = "Select Off";
      this.getField("untitled9").value = "";
      // Clears fields

        }

    }
Correct answer PDF Automation Station

It's using a field value.  You can use the date last saved.  It's called the modified date.  You can obtain it in a script like this:  info.modDate;

You will have to convert both date objects to an identical string format to make the correct comparison.  Enter the following as a document level script:

 

if(util.printd("mm/dd/yyyy",info.modDate)!=util.printd("mm/dd/yyyy",new Date()))
{
this.getField("date").value = "";
this.getField("off").value = "Select Off";
this.getField("untitled9").value = "";
}

 

1 reply

PDF Automation Station
Community Expert
Community Expert
February 1, 2025

Is that a document level script?  If so, you either need to add the function call below it:

clearFormOnNewDate();

or remove the script from the function:

var today = new Date();
        var lastFilledDate = this.getField("date").value; // Replace "date" with the actual field name in your PDF

        if (lastFilledDate != today.toLocaleDateString()) {
      this.getField("date").value = "";
      this.getField("off").value = "Select Off";
      this.getField("untitled9").value = "";
      // Clears fields

        }

Also, is the date field formatted exactly the same as toLocaleDateString()?  You might be better off convering the date field string to a date object then converting that object and today's date object to the same string format to compare.

drslrb
drslrbAuthor
Known Participant
February 1, 2025

Thanks for your response.  I didn't include clearFormOnNewDate(); here by mistake, but I did have it in the PDF. I think I was confused by how this code was working.  I thought it was using the date "date" was last edited vs the date actually typed in "date". Now, I understand how it could be causing an issue.  I will try your suggestion.  

PDF Automation Station
Community Expert
Community Expert
February 1, 2025

It's using a field value.  You can use the date last saved.  It's called the modified date.  You can obtain it in a script like this:  info.modDate;

You will have to convert both date objects to an identical string format to make the correct comparison.  Enter the following as a document level script:

 

if(util.printd("mm/dd/yyyy",info.modDate)!=util.printd("mm/dd/yyyy",new Date()))
{
this.getField("date").value = "";
this.getField("off").value = "Select Off";
this.getField("untitled9").value = "";
}