Download PDF
Download page How to Compare Two Values in Word Templates using IF Statements.
How to Compare Two Values in Word Templates using IF Statements
IF Statements
IF statements (also called IF fields) allow you to compare two values and display document content based on the result of the comparison. When used in a Word template, IF statements are especially useful for comparing the values of Composer merge fields and then displaying the appropriate content.
Example Use Cases
- If an Account’s billing country is not in the United States, display information about international shipping terms. Otherwise, show domestic shipping terms.
- If a Contact’s mailing address is blank, show the billing address from his related Account. If it’s not blank, show the Contact’s mailing address.
- If an Opportunity’s amount is greater than $10,000, show information about a complimentary service package. If not, show nothing.
Nested IF statements in Word, where the else value is another IF statement, work Conga Composer but are not recommended. Conga Technical Support does not support nested IF statements. Conga highly recommends evaluating nested IF statement logic using Salesforce formula fields and then merging the formula field values into the document.
- Watch this video on How to Add IF Statements to Templates.
Create IF Statements in a Word Template
IF statements can use Word merge fields or text-based merge fields, with one exception: the IF statement itself must be a Word merge field. All the fields inside or around the IF statement can be either Word or text-based merge fields, but the IF statement itself must be a Word merge field. Examples using both types of syntax are displayed below. For information on traditional merge fields, see Create Merge Fields in Mail Merge.
IF Statement Structure
“Expression1” Operator “Expression2” “TrueText” “FalseText”
Below is a basic example using Word merge fields as Expressions. The single braces ({ }) on either end of the IF statement indicate the beginning and end of a Word merge field when the field codes are toggled open.
Example:
Components of an IF Field
- Expression1 and Expression2
- Both Expression1 and Expression2 represent values that you want to compare.
- For IF fields used with Composer, the expressions are usually Word or text-based merge fields, text strings, or numbers. Expression1 and Expression2 should be surrounded with quotation marks ("").
- Operator
- A comparison operator. There is always a space before and after the operator.
- Below is a list of available operators:
- TrueText and FalseText
Content that is displayed when the comparison is true (TrueText) or false (FalseText). Each must be surrounded with quotation marks.
If the content of your TrueText or FalseText in Salesforce contains quotation marks ( “ or ” ), replace those quotes with a pair of single quotation marks (' ' instead of " "). Otherwise, Word considers the first quotation mark from your merged data as the end of your text string and the IF statement does not work.
Creating an IF Statement - Best Practices
Follow best practices to ensure the stability and sustainability of your IF statements. In the example below, we display in detail the best practice method for creating IF statements. We also show the syntax for both Word merge fields and text-based merge fields.
Example Scenario: If the Opportunity Amount is greater than $10,000, display information about a complimentary service package. If it's not, do not show anything.
Procedure Step | Result |
Create a traditional merge field with a placeholder name. | «IF_Amount > 10000» |
Open the merge field by toggling field codes. | { MERGEFIELD IF_Amount \* MERGEFORMAT} |
Delete the contents of the field, entering IF instead. | { IF } |
Add Expression1 to the IF field. In our example, Expression1 is the Opportunity Amount field. | Word: { IF “«OPPORTUNITY_AMOUNT»” } Text-based: { IF “{{OPPORTUNITY_AMOUNT}}” } |
Insert your Operator. We're using greater than (>). | Word: { IF “«OPPORTUNITY_AMOUNT»” > } Text-based: { IF “{{OPPORTUNITY_AMOUNT}}” > } |
Insert Expression2. Our example is $10,000. | Word: { IF “«OPPORTUNITY_AMOUNT»” > “10000” } Text-based: { IF “{{OPPORTUNITY_AMOUNT}}” > “10000”} |
Insert TrueText. Our example is a merge field that displays the details of a complimentary service package. | Word: { IF “«OPPORTUNITY_AMOUNT»” > “10000” “«OPPORTUNITY_SERVICE_PACKAGE»” } Text-based: { IF “{{OPPORTUNITY_AMOUNT}}” > “10000” “{{OPPORTUNITY_SERVICE_PACKAGE}}” } |
Insert FalseText. In our example, we don't want to display anything if the Opportunity Amount is less than $10,000, so our FalseText is null. This is represented by a pair of quotation marks. | Word: { IF “«OPPORTUNITY_AMOUNT»” > “10000” “«OPPORTUNITY_SERVICE_PACKAGE»” “” } Text-based: { IF “{{OPPORTUNITY_AMOUNT}}” > “10000” “{{OPPORTUNITY_SERVICE_PACKAGE}}” “” } |
Our finished IF field looks like this:
- Word IF Field: { IF “«OPPORTUNITY_AMOUNT»” > “10000” “«OPPORTUNITY_SERVICE_PACKAGE»” “” }
Text-based IF Field: { IF “{{OPPORTUNITY_AMOUNT}}” > “10000” “{{OPPORTUNITY_SERVICE_PACKAGE}}” “” }
An IF statement that is toggled open should look exactly like the examples above when completed. When initially creating the IF statement, Word includes the following text at the end: \* MERGEFORMAT. This text needs to be removed to ensure the IF statement renders consistently.
When we toggle the field codes closed, the IF statement reverts to the Word merge field with the placeholder name: «IF_Amount»
Using the IF Statement above, let's consider the following example. The Merge Nation Opportunity has an amount of $25,000. Once we merge the Composer template that uses our IF statement, our merged document will look like this:
We display the value of the OPPORTUNITY_SERVICE_PACKAGE field since that is our TrueText. If we use a different Opportunity called Merge Republic, which only has an amount of $8,000, the merged document will show nothing (""), since that is our FalseText.
Caveats
- Checkbox fields
Although the Composer View Data Workbook and the Template Builder show the values of checkbox fields as TRUE and FALSE, these values do not work in an IF statement. Change your value from TRUE to True.
- Example: { IF "<<Checkbox_MergeField>>" = "True" "Yes" "No" }
- Tables
- IF statements placed inside a table row cannot hide the table row when the statement evaluates to show nothing. You will instead have a blank table row. Word IF statements cannot be used to hide rows within a table.
- Salesforce number fields
- Do not put double quotes around a Salesforce number field's corresponding merge field in an IF Statement. If double quotes surround the number field's merge field, it will not merge as expected. See the example below for the correct syntax.
- Example syntax for a number field: { IF <<Item_Number_Field>> > "2" "Free Shipping" "Free Shipping Not Included" }
- When using the AUTOSUM(ABOVE) or SUM(ABOVE) functions to total an international currency field that dynamically formats currency with the currency culture parameter within an IF statement, the merged total amount displays in the running user's currency (determined by their locale), as opposed to the value determined by the currency culture parameter.
- A workaround for this scenario is building a Salesforce formula field that converts the international currency field's value into a number value. Then use an aggregated SUM query for that formula field and use the \# Currency picture switch in the merge field added to the template.
- A workaround for this scenario is building a Salesforce formula field that converts the international currency field's value into a number value. Then use an aggregated SUM query for that formula field and use the \# Currency picture switch in the merge field added to the template.