8 Replies Latest reply on Apr 3, 2009 1:27 PM by (Michael_Frommer)

# What wrong with this script?  Newbie can't figure it out.

Placed at the document level:

function calcTotalMD()
{
// call this function from each MD field, to populate the
// NumberMembersDinner field.
// Calculate the sum of members attending the dinner.
var total = ((this.getField("MD1").value) + (this.getField("MD2").value) + (this.getField("MD3").value) +(this.getField("MD41").value));
// Set the value of the NumberMembersDinner field.
thisEvent("NumberMembersDinner").value = total;
}
• ###### 1. Re: What wrong with this script?  Newbie can't figure it out.
The last line isn't correct.

this.getField("NumberMembersDinner").value = total;
• ###### 2. Re: What wrong with this script?  Newbie can't figure it out.
BTW, you might not need a script to do your total (unless there are other elements in your code that are not here.

You can set your 'NumberMembersDinner ' field to calculate multiple fields (and if you use dot notation for each field name, then you could total an entire group easier).

If you renamed yoiur fields 'M.1' 'M.2' 'M.3' 'M.4', then you could set your total to be the total of field 'M' (Provided there aren't any other fields that start with M. that you don't want totalled.)
• ###### 3. Re: What wrong with this script?  Newbie can't figure it out.
Thanks for taking the time to help, Mark.

I would need to duplicate and modify this script for other sub total fields. Can I use MD.1 instead of M.1, etc.? I would have to do the same for MW.1, NMD.1 and NMW.1. Can I use 3 characters in the Dot notation instead of 1?

How would I have to identify each field (MD, NMD, MW, NMW)?
What is the correct syntax to total a field (is that the same as a group array?)
• ###### 4. Re: What wrong with this script?  Newbie can't figure it out.
You can name the fields anything you want. Whenever you use a dot in the field name, it creates a field "group" for each dot in the name. So if you had, for example, multiple fields named:

InvoiceItems.Parts.1
InvoiceItems.Parts.2
InvoiceItems.Parts.3
InvoiceItems.Labor.1
InvoiceItems.Labor.2
InvoiceItems.Labor.3
InvoiceItems.Markup.1
InvoiceItems.Markup.2
InvoiceItems.Markup.3

you could do a total calculation of Parts by setting the field to calculate the sum of

InvoiceItems.Parts

You could do the same for Labor and Markup. Plus, you could do a total of InvoiceItems by setting a field to calculate the sum of

InvoiceItems

No scripting necessary for these totals. Plus, if you need to add more fields, it will automatically include those in the total as well without making changes (as long as you use the same naming convention for the new fields, e.g. InvoiceItems.Parts.4 InvoiceItems.Parts.5 InvoiceItems.Parts.6 etc.)
• ###### 5. Re: What wrong with this script?  Newbie can't figure it out.
BTW, I forgot to mention, all fields in the 'group' must be of the same type. You cannot have InvoiceItems.Parts.1 be a text field, and InvoiceItems.Parts.2 or InvoiceItems.Markup.1 be a checkbox or comboBox. All fields that start with InvoiceItems. must be the same.
• ###### 6. Re: What wrong with this script?  Newbie can't figure it out.
Mark,

In response to your 1st reply:

I made the change to the last line as you suggested and it still isn't working. When I type the script (without the // Comment text) into the DeBugger the error says: total is not defined.

In response to your 2nd reply, about not posibly needing a script:

Please see my other post at http://www.adobeforums.com/webx/.3bbedaa6.59b87811/0 .

I know it is not proper etiquette to double post. I dont consider this an infraction since this one (in which we are conversing) asked about a specific issue (correct syntax of a script) where that other thread (to which no one had replied) is more of a general issue call for help.

But if you read that other post, at http://www.adobeforums.com/webx/.3bbedaa6.59b87811/0 , it will explain why I want to use a document level script rather than the built-in calculation functionality of the text box. It will even give you a link to where you can find my form.

To summarize, fields such as NumberMembersDinner are not getting updated when fields such as MD1, MD2, etc. are changed.
• ###### 7. Re: What wrong with this script?  Newbie can't figure it out.
I posted in your other thread, but thought I'd ask here as well.

Where are you putting the call to calcTotalMD() ? If you put it in the On Blur event for the entry fields, it should work. (It works for me)
• ###### 8. Re: What wrong with this script?  Newbie can't figure it out.
Mark,

I didn't want to start playing around renaming fields, as you suggest (I'll use that knowledge the next time I need it). I finally got it to work by writting code for each field combination. There is probably an easier way to have written the code, but since I'm so new at this, all that matters is that it works.

function calcTotalAttendees()
{
// Call this function each time AttendeeType is selected.
// Calculate the sum of Members attending the Dinner.
this.getField("NumberMembersDinner").value = ((this.getField("MD1").value) + (this.getField("MD2").value) + (this.getField("MD3").value) + (this.getField("MD4").value));
// Calculate the sum of Non-Members attending the Dinner.
this.getField("NumberNonMembersDinner").value = ((this.getField("NMD1").value) + (this.getField("NMD2").value) + (this.getField("NMD3").value) + (this.getField("NMD4").value));
// Calculate the sum of Members attending the Workshop.
this.getField("NumberMembersWorkshop").value = ((this.getField("MW1").value) + (this.getField("MW2").value) + (this.getField("MW3").value) + (this.getField("MW4").value));
// Calculate the sum of Non-Members attending the Workshop.
this.getField("NumberNonMembersWorkshop").value = ((this.getField("NMW1").value) + (this.getField("NMW2").value) + (this.getField("NMW3").value) + (this.getField("NMW4").value));
}

I call it from the fields that total the attendee type (ie Member, Non-Member) for each event.

Sorry I didnt reply sooner, I had to spend more time on my real job and after work I was too tired in the evening to work on this.