Visual Rule Builder Part 2: Subtotals and Totals

rules.jpgIn a previous article, we discussed how the Visual Rule Builder (VRB) update – it looks like it’s coming next weekend and we’re really excited – makes it super-simple to initialize forms with the current date, personal information etc.

Of course, real-world forms and workflows have many kinds of business requirements and another common one is the need to compute a subtotal and a grand total in a table of line items.

The form might look something like this. There’s a Table with four columns : Item description, Quantity, Unit Price and Subtotal. The Subtotal for each line is computed as the product of Quantity and Unit Price. There’s also a Grand Total which sums the individual line items.

Screen Shot 2017-12-05 at 1.58.35 PM.png

Prior to the VRB, you’d have to write a JavaScript rule as shown below

var tot = 0;
for (var i = 0; i < Quantity.value.length; i++) {
if (Quantity[i].value > 0 && UnitPrice[i].value > 0) {
var subtot = Quantity[i].value * UnitPrice[i].value;
Subtotal[i].value = subtot;
tot += subtot;
} else {
Subtotal[i].value = null;
}
}
GrandTotal.value = tot;

Once again, it’s not wildly complicated but there are a few quirks that could trip up someone who’s not used to coding e.g. a temporary variable is required to compute the Grand Total, there’s a for loop and the repeating fields need to be treated as arrays. The syntax can be a bit confusing, it’s easy enough to make a mistake and hard to debug.

Enter the Visual Rule Builder

With the VRB, it’s simple and anyone can do it. We’ll create two separate rules, one for the subtotals and another one for the Grand Total.

1. Open the VRB and set the condition. This is when we want the rule to fire. In this case, a Subtotal can only be calculated when we know the Quantity and Unit Price so that’s what we setup in the first panel of the VRB wizard.

Screen Shot 2017-12-05 at 2.34.50 PM.png

2. The next step is to define what actions to take when the condition is TRUE. Once again, with the VRB, it’s straightforward. We want to multiple Quantity and Unit Price.  So, we simply Set the Subtotal to the desired expression. We don’t have to worry about the fact that these are repeating fields in a table, there’s no for loop or array indexes to consider.

Screen Shot 2017-12-05 at 2.38.18 PM.png

3. Finally, in the last panel of the VRB wizard, we set the Subtotal to empty when the condition is FALSE. This ensures that the Subtotal will be cleared if Quantity or Price are empty.

Screen Shot 2017-12-05 at 2.39.47 PM.png

4. The last step is to calculate the Grand Total. Finish the above rule, create a new one and open the VRB. There is no Condition required so leave it empty. On the next panel we use a function to set the Grand Total to the Sum of all the Subtotals. That’s it. The SUM () function is smart enough to ignore empty rows and treats them as 0’s.

Screen Shot 2017-12-05 at 2.42.49 PM.png

5. The final rules look like this.

Screen Shot 2017-12-05 at 2.44.35 PM.png

6. Test the form. You’ll see that the Subtotals and Grand Total are automatically computed as desired.

Screen Shot 2017-12-05 at 2.47.17 PM.png

That’s it. Quick and easy – anyone can do it. This VRB update is coming really soon and we’re very excited about it because simplifying JavaScript rules has been a high-priority request from our customers. Contact us for more information and to try out the VRB for your own business forms.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s