• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Custom calculation to sum or allow text

New Here ,
May 29, 2024 May 29, 2024

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();

TOPICS
PDF forms

Views

818

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
May 29, 2024 May 29, 2024

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;

View solution in original post

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 29, 2024 May 29, 2024

Copy link to clipboard

Copied

Where is the definition of the Calcs function?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 29, 2024 May 29, 2024

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?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 29, 2024 May 29, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 29, 2024 May 29, 2024

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 29, 2024 May 29, 2024

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 29, 2024 May 29, 2024

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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 29, 2024 May 29, 2024

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;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 30, 2024 May 30, 2024

Copy link to clipboard

Copied

LATEST

This worked perfectly.  Thank you so much for your help, you saved the day!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 30, 2024 May 30, 2024

Copy link to clipboard

Copied

Copy the function from the old form.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines