Skip to main content
Zeital
Participant
Jul 17, 2019 12:04 AM UTC
Answered

Custom calculation

Hi,

I have a script running but I am running into an issue between using the text fields and checkbox.

Text1 has a default value of 15, this is multiplied by Text2 which the user can set (say for this example 6) + the value set to the checkbox (say 50).

When the Text2 and the checkbox are empty the Total field is displaying "0Off" and if I fill in Text2 with 6 (15*6=90) it will show 90Off and with the checkbox finally checked it then displays the total of 140

or if I change the Total field format to "number" I get an error message whenever one of the fields changes "The value entered does not match the format of the field [ Total ]" and does not 0 back out when the Text2 or checkbox are emptied again.

Here is the code I am putting into the Total field for reference.

event.value = ( this.getField("Text1").value * this.getField("Text2").value ) + this.getField("checkbox").value;

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Jul 17, 2019 9:18 AM UTC

In addition to Bernd's correct remark, you also need to convert the values of the fields to numbers.

Try this code:

var v1 = Number(this.getField("Text1").valueAsString)

var v2 = Number(this.getField("Text2").valueAsString)

var v3 = (this.getField("checkbox").valueAsString=="Off") ? 0 : Number(this.getField("checkbox").valueAsString);

event.value = ( v1 * v2 ) + v3;

3 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Jul 17, 2019 9:18 AM UTC

In addition to Bernd's correct remark, you also need to convert the values of the fields to numbers.

Try this code:

var v1 = Number(this.getField("Text1").valueAsString)

var v2 = Number(this.getField("Text2").valueAsString)

var v3 = (this.getField("checkbox").valueAsString=="Off") ? 0 : Number(this.getField("checkbox").valueAsString);

event.value = ( v1 * v2 ) + v3;

Translate
Zeital
ZeitalAuthor
Participant
Jul 17, 2019 10:26 PM UTC

Cheers, that worked a treat!

Translate
Bernd Alheit
Community Expert
Community Expert
Jul 17, 2019 7:34 AM UTC

The value of a unchecked checkbox is "Off".

Translate