Skip to main content
Inspiring
March 17, 2020
Answered

Calculation to show 0 when 0 is entered otherwise will be blank when SPECIFIC fields are empty.

  • March 17, 2020
  • 2 replies
  • 2077 views

Hello All.

Here is my problem.

Field "A" is populated from a previous calculation.
Next calulation is A x B x C = D.
If B and C is 0 (zero), then D will diplay 0 (zero)

If B & C are left blank, then D will display an empty field

Field A & D are read only

I've a can't seem to finds a script that applies to the above.

I would appreciate any help.

 

Thanks.

This topic has been closed for replies.
Correct answer cliffb42815331

The script its what George_Johnson-9fk31b left.


 

// Custom calculate script
(function () {

    // Get the input field values, as strings
    var sA = getField("A").valueAsString;
    var sB = getField("B").valueAsString;
    var sC = getField("C").valueAsString;

    if (sB && sC) {  // If both of these input fields are not empty...

        // If both of these field values are zero when converted to a number...
        if (+sB === 0 && +sC === 0) {
            event.value = 0;  // Set this field value to zero
            return;
        } else {
            event.value = sA * sB * sC;  // Set this field value to this product
            return;
    }

    } else {
        // Blank this field since B and C inputs are blank
        event.value = "";
        return;
    }

})()

Code works. I replaced with incorrect field names.

Thanks George_Johnson-9fk31b

2 replies

try67
Community Expert
Community Expert
March 17, 2020

This condition is not necessary:

If B and C is 0 (zero), then D will display 0 (zero)

If either of those fields is zero then the result of multiplying them by any other value will be zero, no matter what.

Inspiring
March 17, 2020

It's not about the math. It's about what displays.

B & C are populated by user. BUT if they enter a 0 (zero). The resulting field will show 0 (zero). IF they leave it blank then the resulting field will be blank.

Inspiring
March 17, 2020

In the custom calculation script for D, you could do something like:

 

// Custom calculate script
(function () {

    // Get the input field values, as strings
    var sA = getField("A").valueAsString;
    var sB = getField("B").valueAsString;
    var sC = getField("C").valueAsString;

    if (sB && sC) {  // If both of these input fields are not empty...

        // If both of these field values are zero when converted to a number...
        if (+sB === 0 && +sC === 0) {
            event.value = 0;  // Set this field value to zero
            return;
        } else {
            event.value = sA * sB * sC;  // Set this field value to this product
            return;
    }

    } else {
        // Blank this field since B and C inputs are blank
        event.value = "";
        return;
    }

})();

 

You'd have to change the field names in the getField statements to the actual field names. Be sure to set the field calculation order to what makes sense.

 

Inspiring
March 17, 2020

Sorry.

I get what the script is trying to do but what should I be copying to paste in. I'm getting errors.

The names of my fields are
A=g

B=gg
C=yy

try67
Community Expert
Community Expert
March 17, 2020

It would be helpful to know what the error messages you're getting actually say...

Also, post your full code.