Skip to main content
Participating Frequently
December 31, 2024
Answered

Javascript for custom computed fields not retained

  • December 31, 2024
  • 2 replies
  • 790 views

I am trying to use custom calculation script using javascript to populate a field on the PDF. I select "Custom calculation script", then press the "Edit" button, enter the code below into the editor, then click the "OK" button.  The problem is that the code is never retained and the radio button is reset to "Value is not calculated" on the fields "Calculate" tab. 

 

The code I am using is:

 

var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
this.getfield("RFP Number").value = "HCF"+yyyy+mm+dd;

 

I am using Acrobat (64-bit), version 24.005.20320 

Does anyone have any clue why this is happening and code is not retained?

 

This topic has been closed for replies.
Correct answer Nesa Nurani

I'm not sure how to describe this. My original code as shown did not get an error after pressing OK in the editor. I tried your code as well, no errors after pressing OK, but code is not retained. See the pics below.

 

Step 1: Select "Custom calculation script" and click the "Edit" button.

 

Step 2: Add code and click the OK button.

 

Step 3: The result after pressing OK

 


Are you on Windows or Mac?
Try to create new field and check if the code is retained in new field or even test on new file.

2 replies

PDF Automation Station
Community Expert
Community Expert
December 31, 2024

Even though your code is not correct as @Nesa Nurani explained, it should still be retained.

Nesa Nurani
Community Expert
Community Expert
December 31, 2024

It can't be retained because if he click 'Ok' he will get error and script can't be saved, but there is also a part about radio buttons I did not quite understand where exactly he uses script.

PDF Automation Station
Community Expert
Community Expert
December 31, 2024

I tested the script and it was retained for me.  I'm using Notepad as my external JS editor.  Not sure if that makes a difference.  Regarding the radio button, I think he just means that after it is not retained it defaults back to "Value is not calculated".

 

Nesa Nurani
Community Expert
Community Expert
December 31, 2024

Some parts of the script doesn't work in Acrobat, like padStart.

Try this instead:

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();

dd = (dd < 10 ? '0' : '') + dd;
mm = (mm < 10 ? '0' : '') + mm;

this.getField("RFP Number").value = "HCF" + yyyy + mm + dd;