Expand my Community achievements bar.

SOLVED

Issue with custom calculation script

Avatar

Level 3

I am converting an Adobe DC form to an AEM form, and having an issue with converting a script, and need some help because the syntax check is fine, and do not receive any errors in preview, so not sure what is wrong.

 

I have the following code from the custom calculation script in my Adobe DC form

 

// Initialize variables

var i, v, num = 0, sum = 0;

// Loop through the input fields

for (i = 1; i <= 13; i++) {

  v = +getField("A" + i).value;

  if (v !== 0) {

  // increment the non-blank/zero field counter

  num++;

  // add the field value to the running total

  sum += v;

  }

}

// Calculate the average

if (num) {

  event.value = sum / num;

} else {

  // All fields are empty, so set to blank

  event.value = "";

}

event.value = (event.value - 0.005).toFixed(2);
if (event.value <= 0) event.value = "";

 

and when I add that to the calculate, script for the form in AEM, it appears fine, but nothing appears in the field.

Now all of the "A1 - A13" fields are in separate subforms, so not sure if that makes a difference with the script or not, but wanted you to know how my form is setup.

On another related question, will this form work, or is there any tweaks I need to make if some of the pages containing these "A" fields are hidden, until a button is clicked?  I would think the code would still work, but just wanted to ask to make sure.

 

Thanks in advance for any help?

1 Accepted Solution

Avatar

Correct answer by
Employee

I have fixed the script on Overall Score field on page 6. Now, it is working as expected. Please check and confirm.

Fixed form: https://drive.google.com/file/d/1X8pKKV68ksNXyyjX7EcPHfcJ_TKu88Aj/view?usp=sharing

 

View solution in original post

6 Replies

Avatar

Employee

Can you use the following JavaScript on the calculate event of the field.

 

var i, v, num = 0, sum = 0;

// Loop through the input fields

for (i = 1; i <= 13; i++) {

  v = this.resolveNode("A" + i).rawValue;

  if (v !== 0) {

  // increment the non-blank/zero field counter

  num++;

  // add the field value to the running total

  sum += v;

  }

}

// Calculate the average

if (num) {

  this.rawValue = sum / num;

} else {

  // All fields are empty, so set to blank

  this.rawValue = "";

}

this.rawValue = (this.rawValue - 0.005).toFixed(2);
if (this.rawValue <= 0) this.rawValue = "";

Avatar

Level 3

Thanks, but I just tried that script, and field is still remaining blank when those other fields are updated.

 

Am I doing something wrong?  I am adding it to the javascript calculate script section.

Avatar

Employee

Can you please share your form via Google Drive so that I can check further on this?

Avatar

Correct answer by
Employee

I have fixed the script on Overall Score field on page 6. Now, it is working as expected. Please check and confirm.

Fixed form: https://drive.google.com/file/d/1X8pKKV68ksNXyyjX7EcPHfcJ_TKu88Aj/view?usp=sharing