Skip to main content
Participant
November 29, 2018
Answered

Calculation with Minimum Value

  • November 29, 2018
  • 2 replies
  • 463 views

How do I write a custom calculation for:

Text1 + Text2 + Text3 = Text20

If sum is 0 then value is 0

If sum is any number between 1 - 300 then value is 300

If sum is >300 then value is sum

Here is what is NOT working:

if( Text20 <= 300 ) {

event.value = 300; // value is 300 when sum is less than or equal to 300;

} else {

if ( Text20 == 0 ) {

event.value == 0; // value is 0 when sum is equal to 0;

} else {

event.value = Text20; // else value is the sum;

}

}

This topic has been closed for replies.
Correct answer try67

Use this code as the custom calculation script of your field:

var total = Number(this.getField("Text1").valueAsString) + Number(this.getField("Text2").valueAsString) + Number(this.getField("Text3").valueAsString);

if (total>1 && total<300) total = 300;

event.value = total;

2 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
November 29, 2018

Use this code as the custom calculation script of your field:

var total = Number(this.getField("Text1").valueAsString) + Number(this.getField("Text2").valueAsString) + Number(this.getField("Text3").valueAsString);

if (total>1 && total<300) total = 300;

event.value = total;

Legend
November 29, 2018

Looks about right, except you are missing the compulsory "getField" call.