It is fairly common for forms to have multiple fields on which script wants to take the same action. This article provides a guide on how to best name fields to take easy action on multiple fields.


Let's consider a form that has 10 quantity fields, 10 price fields, and a single total field. Our code would like to sum up a total by multiplying each price field by its associated quantity field. Sometimes form designers will name the fields in a way that correlates them to the area of the form they're on. For instance, you might see something like:

  • FishQty
  • DeerQuantity
  • CowQty
  • SheepCount
  • BirdQty
  • FishPrice
  • DeerPrice
  • CowSpend
  • SheepPrice
  • BirdCost
  • ...and so on


However, this can be a bit challenging to write good code, because your script might look something like this:


function calculateTotal()
{
    var total = 0;
    total += _form.getNumericValue("FishQty") * _form.getNumericValue("CowPrice");
    total += _form.getNumericValue("DeerQuantity") * _form.getNumericValue("DeerPrice");
    total += _form.getNumericValue("CowQty") * _form.getNumericValue("CowSpend");
    total += _form.getNumericValue("SheepCount") * _form.getNumericValue("SheepPrice");
    total += _form.getNumericValue("BirdQty") * _form.getNumericValue("BirdCost");
    // and so on for other fields
    _form.setValue("Total", total.toFixed(2));
}


But this can get a bit unwieldy. However, through the use of clever field naming we can perform these steps a bit easier. One way to do this would be to use numeric field names like this:

  • Qty_1
  • Qty_2
  • Qty_3
  • Qty_4
  • Qty_5
  • Price_1
  • Price_2
  • Price_3
  • Price_4
  • Price_5
  • ...and so on


This would allow us to write code that looks like this:


function calculateTotal()
{
    var total = 0;
    for (var i=1; i<=5; i++)
    {
        total += _form.getNumericValue("Qty_" + i.toString()) * _form.getNumericValue("Price_" + i.toString());
    }
    _form.setValue("Total", total.toFixed(2));
}


And if we ever added additional fields, all we'd have to do is change the "5" in the script. However, a downside to this is that the fields don't have much meaning to someone looking at exported data such as in a CSV or XML file. Instead, let's consider a field naming convention that looks like this:


  • Qty_Fish
  • Qty_Deer
  • Qty_Cow
  • Qty_Sheep
  • Qty_Bird
  • Price_Fish
  • Price_Deer
  • Price_Cow
  • Price_Sheep
  • Price_Bird
  • ...and so on


But, we don't want to revert to code that looks like the first example, so instead we can take advantage of our field naming knowledge like so:


function calculateTotal()
{
    var total = 0;
    // Get a list of all the fields
    var fields = _form.getFieldList();
    // Iterate through all the fields
    for (var i=0; i<fields.length; i++)
    {
        // If we found a quantity field, multiply it by its corresponding cost field and add it to the total
        if (fields[i].startsWith("Qty_"))
        {
            total += _form.getNumericValue(fields[i]) * _form.getNumericValue("Price" + fields[i].split("_")[1]);
        }
    }
    _form.setValue("Total", total.toFixed(2));
}


This works by finding all fields that start with "Qty_" and then splitting that field name into 2 parts. It then knows that there's a corresponding "Price_" field and multiples the two field values. A big advantage of this code is that you can add as many Qty and Price fields to the form as you want and you never have to change your script code.