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

# Calculated field BG color change automatically

Explorer ,
Nov 21, 2023 Nov 21, 2023

Copied

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.

TOPICS
Create PDFs , How to , JavaScript , PDF , PDF forms

Views

165

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 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;}
els``````
...

2 Replies 2
Community Expert ,
Nov 21, 2023 Nov 21, 2023

Copied

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;``````

Report

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

Copied

LATEST

Thank you so much, It's perfect!