Skip to main content
Known Participant
November 21, 2023
Answered

Calculated field BG color change automatically

  • November 21, 2023
  • 1 reply
  • 754 views

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 😞 

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

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;

1 reply

Nesa Nurani
Nesa NuraniCorrect answer
Community Expert
November 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;
Known Participant
November 22, 2023

Thank you so much, It's perfect!