Issue with custom calculation script | Community
Skip to main content
New Participant
February 1, 2022
Solved

Issue with custom calculation script

  • February 1, 2022
  • 1 reply
  • 1496 views

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Mayank_Tiwari

sure, here is the link to the form

 

https://drive.google.com/file/d/11Ds94HjTr2wrZNKYq61_S7b3TqXe97v8/view?usp=sharing

 


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

 

1 reply

Mayank_Tiwari
Employee
February 2, 2022

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 = "";
New Participant
February 2, 2022

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.

Mayank_Tiwari
Employee
February 2, 2022

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