Stefan Cameron on Forms
Building intelligent forms using Adobe LiveCycle Designer

'FormCalc' Category Archive

FormCalc Expressions (Foreach)

A couple of weeks ago, I started a series of posts on FormCalc Expressions. The first ones I convered were the If and For expressions. This time, I thought I would explain the Foreach expression.

Foreach Expression

The Foreach expression is quite similar to the For expression but is meant to be used to achieve a slightly different kind of iteration that may be better suited to your needs rather than just increasing/decreasing the value of a counter (as for a regular For expression). The difference is that the Foreach expression assigns a different value taken from an argument list (list of objects/values) to a variable for each iteration of the loop (and there’s one iteration for each argument in the list).

FormCalc Syntax

foreach Variable in (ArgumentList) do
  ExpressionList
endfor

In this syntax, there’s an ArgumentList. This is simply a comma-separated list of objects/values to assign to the Variable on each iteration.

Based on the syntax, you can do things like set a total field’s value to the sum of subTotal, shipping and taxes fields:

foreach val (subTotal, shipping, taxes) do
  total = total + val
endfor

In this example, each time the expression loops, the variable val assumes the “identity” of a different object in the argument list: First, the subTotal field, then the shipping field and, finally, the taxes field.

JavaScript Syntax

For comparison, here is the equivalent JavaScript examples (note the semicolons and curly braces):

var arr = {0: subTotal, 1: shipping, 2: taxes};

for (key in arr)
{
  total.rawValue += arr[key].rawValue;
}

(In this case, the curly braces are optional but I think this is better style.) It’s certainly more cumbersome in JavaScript using the For…In expression (which is the closest equivalent to FormCalc’s Foreach expression I could find) because it’s only meant for iterating through an object’s properties or an array’s keys (which is what my example is doing).


Posted by Stefan Cameron on October 7th, 2006
Filed under FormCalc,Scripting

FormCalc Expressions (If and For)

In Designer, you can write scripts using one of two languages: FormCalc and JavaScript. If you’re like me, you’re more familiar with JavaScript because it’s very similar to Java and JScript syntax but when it comes to FormCalc, you’re lost. Well, not completely but you find it very difficult to find documentation on the syntax for loops (like While, For, etc.) which are sometimes essential. Maybe you fall-back on JavaScript in those times but, on average, FormCalc scripts execute much faster than JavaScript scripts so why not harness that performance in our forms?

In this post, I’ll explain the syntax of the If and For expressions in FormCalc. Those of you familiar with Visual Basic or VBScript will likely find this more familiar than the equivalent JavaScript syntax (while I’ll include for comparison however this isn’t a post on JavaScript syntax). In future posts, I’ll add to this collection by discussing the syntax for While and ForEach loops.

Conventions

Before we get started, I should explain a few things about the various syntax blocks you’ll find further down in this post (note that this is simplified for the purposes of this article):

  • SimpleExpression: A simple expression like a comparison “i >= 0” that evaluates to true or false or a single entity like “MyVar” (a defined variable) or “5” (a number), for example.
  • ExpressionList: A set of 1 or more expressions, each on a separate line.
  • Square Brackets: A series of keywords (like “if”, “then”, “for”, “while”, etc.), SimpleExpressions and ExpressionLists grouped within square brackets denote an optional part of the syntax.
  • Asterisk Character: Sometimes a closing square bracket is followed by an asterisk (*). This indicates that the optional syntax may be used 0 or more times.
  • Question Mark Character: Sometimes a closing square bracket is followed by a question mark (?). This indicates that the optional syntax must be used 1 or more times.

Variables

I assume you’re probably familiar with declaring variables in scripts but just in case, in FormCalc, you declare variables like this:

var MyVar

and you can even declare it and assign it a value in the same step:

var MyVar = 5

If Expression

Let’s look at one of the most essential of all expressions:

FormCalc Syntax

if ( SimpleExpression ) then
    ExpressionList
[ elseif ( SimpleExpression ) then
    ExpressionList]*
[ else
    ExpressionList]?
endif

Based on the syntax, you can do things like (assuming you’ve declared a variable named “i”):

if (i >= 10) then
  i = i + 10
endif

or

if (i >= 10) then
  i = i + 10
elseif (i >= 5) then
  i = i + 5
  i = i * 2
else
  i = i + 1
endif

Hopefully this illustrates the differences in the syntax indicated by the asterisk and question mark characters.

JavaScript Syntax

For comparison, let’s look at the equivalent JavaScript syntax for the two examples above (note the semicolons and curly braces):

if (i >= 10)
  i = i + 10;

or

if (i >= 10)
  i = i + 10;
else if (i >= 5)
{
  i = i + 5;
  i = i * 2;
}
else
  i = i + 1;

For Expression

Often times, in your scripts, you need to execute some statements a certain number of times or iterate through a collection. In those cases (and many others), a For loop may be useful.

FormCalc Syntax

for Variable = StartExpression
        (upto | downto) EndExpression
            [step StepExpression]?
        do
    ExpressionList
endfor

In this syntax, there’s a StartExpression, and EndEExpression and a StepExpression. These are all similar to SimpleExpressions as described earlier. One thing to note about For loops in FormCalc is that “upto” loop will iterate as long as Variable is less-than-or-equal-to (<=) EndExpression whereas a “downto” loop will keep iterating as long as Variable is greater-than-or-equal-to (>=) EndExpression. Also, the variable Variable is implicitely declared in the For expression.

Based on the syntax, you can do things like add the numbers 1 to 10, inclusively, to a list:

for i = 1 upto 10 do
  MyList.addItem(i)
endfor

and then

var nCount = MyList.#items[0].nodes.length

if (nCount >= 2) then
  for nItem = nCount downto 1 step 2 do
    xfa.host.messageBox( MyList.#items[0].nodes.item(nItem - 1) )
  endfor
endif

which would display the value of each even-numbered list item in reverse sequence using a message box as long as there’s at least one even-numbered list item to display.

JavaScript Syntax

For comparison, here are the equivalent JavaScript examples (note the semicolons and curly braces again):

for (var i = 1; i <= 10; i++)
{
  MyList.addItem(i);
}

(in this case, the curly braces are optional but I think this is better style) and then

var nCount = MyList.resolveNode("#items[0]").nodes.length;

if (nCount >= 2)
{
  for (var nItem = nCount; nItem >= 1; nItem - 2)
  {
    xfa.host.messageBox(
      MyList.resolveNode("#items[0]").nodes.item(nItem - 1) );
  }
}

Posted by Stefan Cameron on September 14th, 2006
Filed under FormCalc