Creating expressions can sometimes be necessary to get the result you want. This blog covers some tips that can help you with creating complex expressions.
I’ve created an example to help understand how the Document Creator engine translates expressions to values. The following sales order has some lines with decimals and a boolean on it. I want to use these 3 fields to manage the VisibleExpression of another field or element. To ensure that the outcome used for the VisibleExpression is correct I’ve added a textbox to make the outcome visible. The result should be TRUE or FALSE, nothing more, nothing less. The expression must comply to the following instruction: Line Amount minus Amount is greater than 0 and “Calc Discount Amount” is True.
So let’s start with the first part: Line Amount minus Amount is greater then 0
Enter expression [SalesLine.LineAmount]-[SalesLine.Amount]>0 and run preview. The result:
Document Creator only handles expressions when put between square brackets [ ]. All the other syntax is handled as text. Since we are using a textbox, Document Creator expects a text expression.
Put [ ] around the expression and the result is fine:
Let’s continue with the second part of the instruction and combine it with the first part: Calc Discount Amount is True.
SalesLine.CalcDiscountAmt is a Boolean field so we just have to combine the two statements. The new expression is:
[[SalesLine.LineAmount]-[SalesLine.Amount]>0] && [SalesLine.CalcDiscountAmt]
As mentioned earlier the expression needs to be between brackets. Put some brackets around it and preview the report:
An error occurs.. Invalid Expression..
The expression level is too deep. Document Creator doesn’t know in which order it has to process the nested expressions. How can we fix it?
[([SalesLine.LineAmount]-[SalesLine.Amount]>0) && [SalesLine.CalcDiscountAmt]]
Replace the [ ] of the initial statement to ( ) and now the result is exactly how it should be:
You can even put some extra ( ) around the ([SalesLine.LineAmount]-[SalesLine.Amount]) statement to be sure that this is first executed before the “greater then” expression.
Now that we have the output we were looking for, we can use it to set the VisibleExpression. Add a picture to the report and put the expression we have created in the VisibleExpression property. Again an error occurs. The same one as in example 2. This has to do with the fact that a Textbox expects Text and the VisibleExpression expects an expression. Just remove the outer brackets from the VisibleExpression and everything should work. The expression looks like this:
([SalesLine.LineAmount]-[SalesLine.Amount]>0) && [SalesLine.CalcDiscountAmt]
There are a few things to be aware of when creating expressions in Document Creator. But once you learn how to breakup an expression, and how to use brackets to make sure it evaluates in proper order, you can do really cool things and create pretty complex expressions which help to develop intelligent reports.
Good luck ‘expressing’ yourself!