Conditional logic (IF fields)
Using Conditional Logic on Invoice Templates
The number of invoice templates your business needs can increase with every new payment method, seasonal discount, and more. Consolidate invoice templates by using conditional logic. Based on customer information or charge attributes, you can display different pieces of information on an invoice. For example, you can display different instructions based on a customer’s payment method:
What Are IF Fields?
IF fields contain IF/THEN logic that allows you to customize what is displayed on a customer's invoice based on the value of a merge field. For example, you can display the price of an invoice item as a numerical dollar amount, or, if the amount is zero, display "FREE".
See Customize Invoice Templates Using Word Mail Merge for information on how to insert merge fields into an invoice template.
What IF Fields Can't Do
- IF fields cannot hide or display specific table columns or rows.
- IF fields cannot hide or display recursive tables.
IF Field Syntax
IF fields test the value of a merge field (the test condition), then display different results depending on whether the test result is true
or false
. The format of an IF field is:
{ IF [Test Condition] [Result Displayed if TRUE] [Result Displayed if FALSE] }
The result displayed can be text, the value of a merge field, a table, or nothing.
See Merge Fields Supported in IF Fields on Invoice Templates for information on which merge fields you can use with IF fields.
For more information on using IF fields, go to the Microsoft Office Support Center.
Having nested IF conditions in invoice templates might lead to very slow invoice PDF generation. See Avoid Complex Invoice Templates for more information.
Example Use Cases
Example 1: Display Text or a Merge Field Value
The IF field in this example tests InvoiceItem.ExtendedPrice
to see if the value of the merge field equals 0
. If the value equals 0
, the test returns true
and the IF field displays "FREE" on the invoice. If the value of the field does not equal 0
, then the IF field displays the value of the merge field InvoiceItem.ExtendedPrice
on the invoice instead.
{ IF {MERGEFIELD InvoiceItem.ExtendedPrice \* MERGEFORMAT} = 0 "FREE" {MERGEFIELD InvoiceItem.ExtendedPrice \* MERGEFORMAT} }
Add the \*
MERGEFORMAT
switch to the end of all fields, including IF fields. The switch ensures that any formatting you apply to a result displayed by a field is retained in subsequent results.
Here is what the invoice might look like when the IF field in this example is used in the TOTAL column of a Rate Plan Summary table:
RATE PLAN SUMMARY | ||||
---|---|---|---|---|
Subscription Identifier | Rate Plan Name | Subtotal | Tax | TOTAL |
A- S00000010 |
Data Plan |
$39.99 |
$0.00 |
$39.99 |
A- S00000010 |
Cable Plan |
$0.00 |
$0.00 |
FREE |
A- S00000011 |
Data Plan |
$19.99 |
$0.00 |
$19.99 |
A- S00000011 |
Cable Plan |
$0.00 |
$0.00 |
FREE |
Example 2: Display Nothing or a Merge Field Value
The IF field in this example tests Invoice.Discount
to see if the value of the merge field equals 0
. If the value equals 0
, the test returns true
, and the IF field does not display anything on the invoice. If the value of Invoice.Discount
does not equal 0
, the test returns false
, and the IF field displays "You saved $X this month" (where X is the value of Invoice.Discount
).
{ IF {MERGEFIELD Invoice.Discount \* MERGEFORMAT} = 0 "" "You saved {MERGEFIELD Invoice.Discount \* MERGEFORMAT} this month." }
The IF field displays nothing if the result is two consecutive double quotes (""
).
Here is what the invoice might look like when this IF field is used to display the Invoice Discount in an Invoice Summary table:
INVOICE SUMMARY | |
---|---|
Invoice Number |
INV-00000123 |
Subtotal |
$80.00 |
Tax |
$16.00 |
TOTAL |
$96.00 |
Invoice Discount |
You saved $20.00 this month |
Example 3: Hide Empty Tables
If you want to hide an empty table on a billing document, you can use the TableNameTable.Size merge field to control its visibility.
For example, the following IF clause can be used to hide an empty tax summary table from an invoice PDF file.
{ IF “{MERGEFIELD TaxSummaryTable.Size \* MERGEFORMAT}” = "0" "" "
TAX SUMMARY | |||
---|---|---|---|
Tax Name | Tax Rate | Tax Rate Type | Tax Amount |
{MERGEFIELD TableStart:TaxSummary \* MERGEFORMAT}{MERGEFIELD TaxSummary.TaxName \* MERGEFORMAT} | {MERGEFIELD TaxSummary.TaxRate \* MERGEFORMAT} | {MERGEFIELD TaxSummary.TaxRateType \* MERGEFORMAT} | {MERGEFIELD TaxSummary.TaxExtendedAmount \* MERGEFORMAT}{MERGEFIELD TableEnd:TaxSummary \* MERGEFORMAT} |
" \* MERGEFORMAT }
If tax summary data exists, the tax summary table is displayed as follows. Otherwise, the table is hidden.
TAX SUMMARY | |||
---|---|---|---|
Tax Name | Tax Rate | Tax Rate Type | Tax Amount |
tax1 | 0.1 | Percentage | $10.00 |
How to Create an IF Field
This example shows you how to construct the following IF field:
{ IF {MERGEFIELD
InvoiceItem.ExtendedPrice \* MERGEFORMAT} = 0 "FREE" "" \*
MERGEFORMAT
}
The IF field displays the text "FREE" if the value of InvoiceItem.ExtendedPrice equals 0. Otherwise, the IF field displays nothing.
To construct this IF field:
- Move the cursor to where you want to insert the IF field. Press Ctrl + F9 (or command + fn + F9 on a Mac) to insert curly braces. Then type
IF
into the braces:
{ IF }
IF fields are contained within curly braces. You must insert the curly braces into your invoice template by pressing Ctrl + F9 (or command + fn +F9 on a Mac). You cannot just type the curly braces.
- Type a space after
IF
, then insert the InvoiceItem.ExtendedPrice merge field. Insert the merge field as described in Customize Invoice Templates Using Word Mail Merge. You cannot just type the merge field into the curly braces.
{ IF
{MERGEFIELD InvoiceItem.ExtendedPrice
\* MERGEFORMAT}
}
If the merge field formatting appears different to the example, you may be in the field result view. To toggle to the field code view, press Alt + F9 (or option + fn + F9 on a Mac).
- Type in the rest of the merge field:
{ IF
{MERGEFIELD InvoiceItem.ExtendedPrice
\* MERGEFORMAT}
= 0
"FREE" "" \*
}MERGEFORMAT
You must insert a space before and after the operator. Operators include =
, <, >
, <=, and >=
.