4 Replies Latest reply on May 31, 2012 12:46 PM by gkaiseril

# Custom Calculation in Calculated Field

How do I create a customized calculation in a calculated field?  For example, I would like to average fields that only have a numerical value, because there are other choices in the fields included in the calculation such as 1, 2, 3, 4, 5, N/A.  If the field value is N/A, I don't want it in the final averaged value.

• ###### 1. Re: Custom Calculation in Calculated Field

And how exactly are you getting "N/A" in a numerically formatted field?

The following custom script can will compute only numeric values:

// array of field names to process

var aFieldNames = new Array("Text.0", "Text.1", "Text.2", "Text.3", "Text.4");

// value of the named field

var sFieldValue;

// count of processed items

var nCount = 0;

// sum of processed items

var nSum = 0;

// process fields named in array

for(i = 0; i < aFieldNames.length; i++) {

// get the value for a field

sFieldValue = this.getField(aFieldNames[i]).valueAsString;

// process only number values

if(isNaN(Number(sFieldValue)) == false && sFieldValue != "") {

nCount++; // increment count

nSum += Number(sFieldValue); // add to sum

} // end process numeric values

} // end process a field name

event.value = ""; // clear result value

if(nCount > 0) {

// non-zero divisor so we can compute the average

event.value = nSum / nCount;

}

Modify the list of field names as necessary.

• ###### 2. Re: Custom Calculation in Calculated Field

Do I just copy and paste this into the java script box?

• ###### 3. Re: Custom Calculation in Calculated Field

You are awesome!!! Thanks so much!!!

• ###### 4. Re: Custom Calculation in Calculated Field

You cut and paste the code into the custom calculation script for the field in which you want the answer to appear. You will have to change the field names.