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

Calculated field BG color change automatically

Explorer ,
Nov 21, 2023 Nov 21, 2023

Hey guys. I have a query.

I have 3 fields = A, B and C

C = B/A. This is the simplified calculation for C.

 

Now I want C field to change its bg color to green when B is greater than A

and if B is less than A, then red.

 

I have tried so many things and formulae but unable to succeed, please help 😞 

TOPICS
Create PDFs , How to , JavaScript , PDF , PDF forms
580
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Nov 21, 2023 Nov 21, 2023

If you use simplified notation for division, you will get an error when field A is empty.
Instead, try something like this as custom calculation script of field "C":

var a = Number(this.getField("A").valueAsString);
var b = Number(this.getField("B").valueAsString);

if(!isNaN(a) && !isNaN(b) && a !== 0)
event.value = b/a;
else
event.value = "";

if((!isNaN(a) && !isNaN(b)) && (a !== 0 && b !== 0)){
if(b>a)
event.target.fillColor = color.green;
else if(b<a)
event.target.fillColor = color.red;}
else
event.target.fillColor = color.transparent;

View solution in original post

Translate
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 21, 2023 Nov 21, 2023

If you use simplified notation for division, you will get an error when field A is empty.
Instead, try something like this as custom calculation script of field "C":

var a = Number(this.getField("A").valueAsString);
var b = Number(this.getField("B").valueAsString);

if(!isNaN(a) && !isNaN(b) && a !== 0)
event.value = b/a;
else
event.value = "";

if((!isNaN(a) && !isNaN(b)) && (a !== 0 && b !== 0)){
if(b>a)
event.target.fillColor = color.green;
else if(b<a)
event.target.fillColor = color.red;}
else
event.target.fillColor = color.transparent;
Translate
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
Explorer ,
Nov 22, 2023 Nov 22, 2023
LATEST

Thank you so much, It's perfect!

Translate
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