Skip to main content
Participating Frequently
November 11, 2021
Answered

Creating Javascript to conditionally format a fillable form field

  • November 11, 2021
  • 1 reply
  • 4337 views

Hi - I've created a PDF Form with Acrobat. I'm automatically filling the form fields with data from a SmartSheet.  I have some fields that I need to change the background fill color of depending on the content of the field, once filled. For instance, if the field value is "Green", I would want to change the background fill color of the field to Green, etc.

 

Would this be a Custom Format Script? It seems like it would be. 

Can someone provide some assistance on what this code would look like?  This is my first crack at using Javascript in Acrobat, so a bit lost.  Here is my first attempt:

var status = this.getField("Risk/Issue 1 - Status Color");
if (status.value = "Green")
status.fillColor = color.green

 

But that results in the following error:

InvalidSetError: Set not possible, invalid or unknown.
Field.value:2:Field Risk/Issue 1 - Status Color:Format

 

I seem to be stuck at the starting gate. Any assistance is appreciated.

 

Tony

This topic has been closed for replies.
Correct answer try67

try67 - well, THAT definitely made some progress!  The background color now changes, as expected based on the changing input text, per the scripting.  HOWEVER, even though that works as expected when entering data manually, it is not being picked up when generating a document with these fields automatically filled from an external data source.  Perhaps this has to do with WHEN the Javascript is running? Is there a way to adjust how/when the JS runs during a document generation process?

I'm using the Document Builder feature that is available in SmartSheet.com.  I know that once we are talking about an integration between two technologies, things like this may start to get murky, but I'm hoping to find the magic combination of settings to get this to work.  VERY close now thanks to your help.

 

Tony


Did you place the code above as a calculation script and added the doc-level command I provided? If so, after importing the data, save the file, close it and then re-open and the colors should change.

1 reply

tackytonyAuthor
Participating Frequently
November 11, 2021

Ok, so I'm making some progress.  I've updated my Custom Format Script as below:

 

var status = this.getField("Current Status Color").value;

if (status == "Green") {
     event.target.fillColor = color.green;
}
else {
     if (status == "Yellow") {
          event.target.fillColor = color.yellow
     }
     else if (status == "Red") {
          event.target.fillColor = color.red
     }
     else if (status == "Blue") {
          event.target.fillColor = color.blue
     }
}

 

This "sort of" works. 

1)For the automated form fill from SmartSheet, it doesn't do anything.

2)However, if I open the PDF in Acrobat and test it by manually typing in the field, it will change color - BUT only once I click into the field. If I click out of the field, the background color goes back to transparant.

 

I'm not sure if the two behavious are related, but it seems probable.  Any suggestions on next steps?

 

Thanks,

Tony

try67
Community Expert
November 11, 2021

Move the code to be a calculation script and add the following command as a doc-level script (NOT inside a function):

this.calculateNow();

After importing the data save the file, close it and then re-open it and the colors should appear. You must also make sure to disable the Fields Highlights for the colors to show up.

tackytonyAuthor
Participating Frequently
November 11, 2021

Via Tools - JavaScript - Document JavaScripts.


try67 - thank you very much. Unfortunately, I am getting the same behavior. The background fillColor still only shows up when I click inside the field, and doesn't show up at all, of course, when generating the PDF from automated data source.

I've moved the script to a Custome Calculation Script.

I've added a Document JavaScript with 

this.calculateNow(); 

Any other suggestions?