Define and use variable in custom calculation

New Here ,
Oct 10, 2017

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

595

Likes

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

Define and use variable in custom calculation

New Here ,
Oct 10, 2017

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

596

Likes

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
Oct 10, 2017 0
Adobe Community Professional ,
Oct 10, 2017

Copy link to clipboard

Copied

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.

Likes

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
Reply
Loading...
Oct 10, 2017 0
New Here ,
Oct 10, 2017

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Oct 10, 2017 0
Engaged ,
Oct 10, 2017

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Oct 10, 2017 0
Most Valuable Participant ,
Oct 10, 2017

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Oct 10, 2017 0
New Here ,
Oct 10, 2017

Copy link to clipboard

Copied

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;

Likes

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
Reply
Loading...
Oct 10, 2017 0