Skip to main content
Participant
May 29, 2024
Answered

Custom calculation to sum or allow text

  • May 29, 2024
  • 1 reply
  • 1782 views

I'm creating a form and would like to have a field that either calculates the sum of three previous fields, or allows the user to type text.  Specifically, each row has breakfast, lunch, dinner, and total fields.  In the total field I would like the sum of breakfast, lunch, and dinner OR allow the user to type an amount.  I'm modeling off an old form that has the below in the custom calculation script.  It works on the old form, but not the new one.  The new form doesn't sum the fields and only allows text entry.  Help please 🙂

 

 

var B = this.getField("BREAKFASTRow1").value;

if(B>0) event.vaule = Calcs();

var L = this.getField("LUNCHRow1").value;

if(L>0) event.vaule = Calcs();

var D = this.getField("DINNERRow1").value;

if(D>0) event.vaule = Calcs();

This topic has been closed for replies.
Correct answer Nesa Nurani

I guess either works.  If any field has an amount entered, I would like the sum entered in the "total" field, however if no amounts are entered, I would like the "total" field to be "unlocked" in a sense so any amount can be entered by the user.  Hopefully that makes sense.  I appreciate your help.

 


Try this:

var a = Number(this.getField("BREAKFASTRow1").valueAsString);
var b = Number(this.getField("LUNCHRow1").valueAsString);
var c = Number(this.getField("DINNERRow1").valueAsString);

if(a || b || c)
event.value = a+b+c;

1 reply

try67
Community Expert
Community Expert
May 29, 2024

Where is the definition of the Calcs function?

Participant
May 29, 2024

That's a great question.  I'm wondering if the original form is pulling from another document that I'm not aware of?  (If that's even possible...)  Can I add the cals function after the original script?

 

Nesa Nurani
Community Expert
Community Expert
May 29, 2024

Also, it's not:  event.vaule   it should be:  event.value