Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
Where is the definition of the Calcs function?
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Also, it's not: event.vaule it should be: event.value
Copy link to clipboard
Copied
Great catch- I'm surprised the original script was functioning. I've corrected each instance. Can I follow that script with a simple calculation to sum the three amounts, is any field is greater than 0? Can you help me with what that would look like?
Copy link to clipboard
Copied
You want to calculate all 3 fields if any of them is greater than 0 or only calculate those that are greater than 0?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
This worked perfectly. Thank you so much for your help, you saved the day!
Copy link to clipboard
Copied
Copy the function from the old form.