• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

How to change the field background color and text depending on condition

Contributor ,
Nov 17, 2020 Nov 17, 2020

Copy link to clipboard

Copied

Good day!

 

I have a computed field in fillable PDF (Total_Percentage is the field name).

 

I waht the background color of the field and text to change depending on the following conditins:

 

If Total_Percentage is < 100, I want the background to be yellow and text to be black

If Total_Percentage is = 100, I want the background to be green and text to be white

If Total_Percentage is > 100, I want the background to be red and text to be white.

 

I search in YouTube for solution and saw one but cannot make it work.

 

Thank you!

TOPICS
Create PDFs , PDF forms

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 18, 2020 Nov 18, 2020

Try this code  as validation script:

if(event.value <= 99){
event.target.fillColor = color.yellow;
event.target.textColor = color.black;}
else if (event.value == 100){
event.target.fillColor = color.green;
event.target.textColor = color.white;}
else if(event.value >= 101){
event.target.fillColor = color.red;
event.target.textColor = color.white;}

Votes

Translate

Translate
Community Expert ,
Nov 17, 2020 Nov 17, 2020

Copy link to clipboard

Copied

You can use a custom calculation script in the Total_Percentage field:

 

var f = event.target;

// If Total_Percentage is < 100, I want the background to be yellow and text to be black

if (f.value <= 99) {
f.fillColor = color.yellow;
f.textColor = color.black;
} else {

// If Total_Percentage is = 100, I want the background to be green and text to be white
if (f.value === 100) {
f.fillColor = color.green;
f.textColor = color.white;
} else {

// If Total_Percentage is > 100, I want the background to be red and text to be white.

if (f.value >= 101) {
f.fillColor = color.red;
f.textColor = color.white;
}
}
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 17, 2020 Nov 17, 2020

Copy link to clipboard

Copied

Just to add up, if you have field formated as percentage change numbers in code:

99 to 0.99

100 to 1

101 to 1.01

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 18, 2020 Nov 18, 2020

Copy link to clipboard

Copied

Thanks for replying.

I tried the solution you have provided.

First, I could not add the script in "Custom calculation script" because I am using "Value is the +sum of the following fields| to compute for the Total_Percentage.

I tested putting the script in "Run custom validation script" under the Validate tab.and these are the result.

I don't know if this is the result of putting the script you have provided in "Run custom validation script"

 

Also Nasa commented on the format I used to defined the percentage. I defined the percentage fields as numeric (whole number) without decimal places.

Test Result.jpg

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 18, 2020 Nov 18, 2020

Copy link to clipboard

Copied

Try this code  as validation script:

if(event.value <= 99){
event.target.fillColor = color.yellow;
event.target.textColor = color.black;}
else if (event.value == 100){
event.target.fillColor = color.green;
event.target.textColor = color.white;}
else if(event.value >= 101){
event.target.fillColor = color.red;
event.target.textColor = color.white;}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 18, 2020 Nov 18, 2020

Copy link to clipboard

Copied

LATEST

Hi Nesa,

 

The solution you have provided works. Thank a lot.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines