5 Replies Latest reply on Oct 10, 2017 11:31 PM by bcs marketing

    Define and use variable in custom calculation

    bcs marketing Level 1

      The form I'm working on has 30-some row with multiple similar calculations per row, so I'm hoping to make life easier for myself. Each row has its own prefix for the field names and the rows contain (mostly) the same fields.

       

      I have the following simple calculation:

      if (getField("rowSomething-FieldName").value=="Yes") event.value = getField("rowSomething-Number").value * getField("rowSomething-Price").value;

      if (getField("rowSomething-FieldName").value!="Yes") event.value = "0";

       

      rowSomething-FieldName is a checkbox, rowSomething-Number contains a quantity and rowSomething-Price contains ... a price.

       

      What I'd like to do is declare a variable that equals rowSomething and then call that in the calculation. That way, when I copy & paste the code, I only need to edit the row's name once instead of 4 times. I'm no coder, however, so I can't get it done and most examples I've found are much more complex than this.

        • 1. Re: Define and use variable in custom calculation
          gkaiseril MVP & Adobe Community Professional

          It might even be possible to derive the row number form field if the form field for the result of the calculation is in the row of the fields used for the calculation and the row names have been systemically named. One method of naming rows is to use hierarchical names.

          • 2. Re: Define and use variable in custom calculation
            bcs marketing Level 1

            The row names are based on the products' names, so no logic there unfortunately.

            • 3. Re: Define and use variable in custom calculation
              servitor Adobe Community Professional

              This version could be copied and pasted, just changing the rownumber once.

               

              var rownumber = 5;

              var rowSomethingFieldName = "row" + rownumber + "FieldName"; 

              var rowSomethingNumber = "row" + rownumber + "Number"; 

              var rowSomethingPrice = "row" + rownumber + "Price"; 

              if (getField(rowSomethingFieldName).value=="Yes") event.value =getField(rowSomethingNumber).value * getField(rowSomethingPrice).value;

              if (getField(rowSomethingFieldName).value!="Yes") event.value = "0";

               

              Image 1215.png

              • 4. Re: Define and use variable in custom calculation
                try67 MVP & Adobe Community Professional

                Copying and pasting code is usually a bad idea. Instead, you should use a function and pass the variable part of the name to it as a parameter. You then call that function from the calculation event of each field. So the generic function would be something like this:

                 

                function calcRow(rowNumber) {
                    if (getField(rowNumber+"-FieldName").value=="Yes")
                        event.value = getField(rowNumber+"-Number").value * getField(rowNumber+"-Price").value;
                    else
                        event.value = "0";
                }
                

                 

                And you would call it like this:

                 

                calcRow("abcd");
                
                • 5. Re: Define and use variable in custom calculation
                  bcs marketing Level 1

                  I've managed to get the codes working using only one or two variables I need to enter manually, thereby reducing the number of locations I need to edit per row/column. This lets me copy & paste the code quite easily.

                   

                  The comments that are shown below aren't in the actual codes I use, in case this isn't the correct way to add comments and someone wants to correct me on that

                   

                  Discount field:

                  var discount = "35"; // max. discount

                  if (event.value>discount ) app.alert("Maximum discount for this product is "+discount+"%") && (event.value=discount);

                   

                  Net price (standard price - discount):

                  var net = "31.54" // standard price

                  var field = "rowname"

                  if (getField(field).value != "Yes") event.value = net // set to zero if product is not active

                  if (getField(field).value == "Yes") event.value = (net) - (net * (this.getField("discount-"+field).value / 100));

                   

                  Cost per (subscription) period:

                  var field = "rowname"

                  if (getField(field).value != "Yes") event.value = "0"; // set to zero if product is not active

                  if (getField(field).value == "Yes") event.value = getField("quantity-"+field).value * this.getField("net-"+field).value;

                   

                  Annual cost:

                  var field = "rowname"

                  if (getField(field).value != "Yes") event.value = "0"; // set to zero if product is not active

                  if (getField(field).value == "Yes") event.value = getField("period-"+field).value * getField("periods").value;