Trouble getting a field value for a calculation
Copy link to clipboard
Copied
I am working on a form to do some simple calculations based on quantity and price of several fields.
In the form, each product could have a single quantity or multiple quantities (if there are product variations such as color, size, etc).
I have set up the field so that each product group has a unique name, with the various quantity boxes having a suffix.:
Ball Price 001
Ball Quantity 001-1
Ball Quantity 001-2
etc...
Ball Total 001
The script then loops through all fields, finds the appropriate quantity fields based off the event field then should put them into a total quantity variable.
However it is not functioning (returns undefined).
Here is the code I have in the custom calculation:
// On The Product Calculation Sections
var s = event.target.name;
var suffix = s.substring(s.length-3);
var prefix = s.substring(0,5);
var price = this.getField(prefix + "Price " + suffix).value;
var quantity = 0;
for (var i=0; i<this.numFields; i++)
{
var fname = this.getNthFieldName(i);
var newName = fname.substring(0,fname.length-2);
if(newName == prefix + "Quantity " + suffix)
{
console.println(newName); //THIS WORKS AND SHOWS THE CORRECT FIELDS
quantity.value += newName.value; //DOES NOT WORK
}
}
event.value = quantity * price; //DOES NOT WORK
And here is a link to the form as I have set it up currently (only 2 products but will have hundreds). This may be easier to see how it is set up
Since there will be hundreds of fields to loop through each time a quantity is updated, perhaps there could be a better way to achieve what I am trying?
Cheers
Copy link to clipboard
Copied
1. There are two errors in line 14:
- quantity is a number, not a field, you should access its value directly, it has no "value" property.
- newName is the name of a field, not an actual Field object.
So replace that line with:
quantity += this.getField(newName).value;
2. If the names of your fields are consistent there's no need to iterate over all the fields in the file. You can access them directly.
You're already doing it with the Price field, so why aren't you doing it with the Quantity field, too?
Copy link to clipboard
Copied
Thanks for the reply.
1) It still seems to be putting out an error in that field with the updated code:
2) The reason is that each product could have between 1 to 8 quantities. So I though it easiest to iterate over all fields to get this rather than manually specify how many of each quantity a product has.
Copy link to clipboard
Copied
1. The field name you specified is incorrect.
2. Then just run a loop from 1 to 8. There's really no need to iterate over all the fields in the file.