7 Replies Latest reply on Jun 13, 2014 8:02 AM by George_Johnson

# Custom calculation help

I am getting a syntax error with this formula. SyntaError; missing; before statement1: at line 2  Can someone assist.  Thank you.

Var v1 = ( this.getField(“Total1”).value + this.getField(“Total2”).value + this.getField(“Total3”).value + this.getField(“Total4”).value + this.getField(“Total5”).value + this.getField(“Total6”).value + this.getField(“Total7”).value + this.getField(“Total8”).value + this.getField(“Total9”).value + this.getField(“Total10”).value + this.getField(“Total11”).value + this.getField(“Total1”).value + (this.getField(“Total2”).value );

If (v1 > 40) event.value = 40;

else event.value = (v1);

• ###### 1. Re: Custom calculation help

You're using a non-standard type of quotes. you should use only this kind: "

• ###### 2. Re: Custom calculation help

Also, to define a new variable you need to use "var", not "Var"...

• ###### 3. Re: Custom calculation help

Two other things:

1. Instead of "If" you need to use "if".

2. Since you're apparently doing numeric addition of the field values, you should explicitly convert each one to a number. Otherwise, if any field is blank its value will be an empty string, which will cause string concatenation instead of addition. For example:

1 + 2 + "" + 4 + 6 will result in the string "346" instead of the number 13. You can convert each value to a number using the unary + operator, so that one statement code could be:

var v1 = +getField(“Total1”).value +

+getField(“Total2”).value +

+getField(“Total3”).value +

+getField(“Total4”).value +

+getField(“Total5”).value +

+getField(“Total6”).value +

+getField(“Total7”).value +

+getField(“Total8”).value +

+getField(“Total9”).value +

+getField(“Total10”).value +

+getField(“Total11”).value +

+getField(“Total1”).value +

+getField(“Total2”).value;

Note that this adds Total1 and Total2 twice, which I suspect is an error.

• ###### 4. Re: Custom calculation help

I have this statement working correctly.  Can you help with another issue?  I have the Regular hours and overtime hours calculating correctly.  The Total hours is not reporting correctly.  The field is acting as if there is already a value present and will not computer correctly.

Here is the TotalReg script

var v1 = (this.getField("Total1").value + this.getField("Total2").value + this.getField("Total3").value + this.getField("Total4").value + this.getField("Total5").value + this.getField("Total6").value + this.getField("Total7").value + this.getField("Total8").value + this.getField("Total9").value + this.getField("Total10").value + this.getField("Total11").value +  this.getField("Total12").value + this.getField("Total13").value);

if (v1>40)event.value=40;

else event.value=(v1)

TotalOT script

var v2 = (this.getField("Total1").value + this.getField("Total2").value + this.getField("Total3").value + this.getField("Total4").value + this.getField("Total5").value + this.getField("Total6").value + this.getField("Total7").value + this.getField("Total8").value + this.getField("Total9").value + this.getField("Total10").value + this.getField("Total11").value + this.getField("Total12").value + this.getField("Total13").value);

event.value=v2-this.getField("TotalReg").value

TotalWeek script

event.value = this.getField("TotalReg").value + this.getField("TotalOT")

What do I need to do to make sure the TotalWeek field computes correctly?

• ###### 5. Re: Custom calculation help

George:

Thanks for you reply.  Fields are setup as number format.  However, I appreciate the advise as I am new to JS scripting.

• ###### 6. Re: Custom calculation help

Even if they are formatted as numbers, when the field is blank JavaScript treats the value as character string.

The '+" operator is both additive and concatenation operator,

JavsScript add two values when both are numeric values and concatenates if either or both appear to be a character sting. So the prevailing wisdom is to force the values to numbers when one want to sum a series of fields. A null value when forced to a number is zero.

• ###### 7. Re: Custom calculation help

Fields are setup as number format.  However, I appreciate the advise as I am new to JS scripting.

Unless you've set up the fields so that they can never be blank, you really should do what I suggested and avoid a common programming mistake.