Skip to main content
Participant
October 24, 2016
Question

Need help writing javascript

  • October 24, 2016
  • 1 reply
  • 445 views

I need help writing javascript where I am dividing 2 fields and the answer is in a percentage.  I keep getting an error message and I know it is because I need to add javascript telling what to do when the answer is zero, but I am unsure how to do that.  This is what I currently have written in the Calculate tab under custom calculation script:

event.value = this.getField("Division 1 - 1").value / this.getField("Grand Total 1").value

and in the Format tab I have the category set to percentage.

The error I get is "The value does not match the format of the field"

Any help is much appreciated!  Thank you!!!

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
October 25, 2016

This issue was discussed here many times in the past... Basically this error happens when the divisor is zero, which is an illegal operation.

To prevent it you can do something like this:

var a = Number(this.getField("Division 1 - 1").value);

var b = Number(this.getField("Grand Total 1").value);

if (b==0) event.value = "";

else event.value = a/b;

Participant
October 25, 2016

Thank you.  I have seen this question several times and tried to break down the answers to work for me, but I am not familiar with java script at all and was having a hard time.  I appreciate your help.