If statements in word documents

If Statements / If Fields / Comparison 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, will work with Conga Composer but are not recommended. Conga Technical Support will not assist with 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. This means that all the fields inside or around the IF statement can be either Word or text-based merge fields, but again, the IF statement itself must be a Word merge field.  Following are examples of both types of syntax.

Syntax

{IF "Expression 1" Operator "Expression 2" "TrueValue" "FalseValue"}

Below is a very 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 have been toggled open.

{IF "{{Opportunity_Amount}}" > "100000 "Big Deal Opptye" "Normal Oppty"}

Components of an IF Field

Expression1 & Expression2:

  • Both Expression1 and Expression2 represent fields and values that you want to compare.
  • 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.
  • Supported operators:

TrueValue & FalseValue

  • Content that is shown when the comparison is true (TrueValue) or false (FalseValue). Each must be surrounded with quotation marks.

If the content of your TrueValue or FalseValue contains quotation marks ( “ or ” ), you need to replace those quotes with two pairs of single quotation marks (‘’ ‘’ instead of “ ”). Otherwise, Word will consider the first quotation mark from your merged data as the end of your text string and the IF statement will not work.

Creating an IF Field — Conga Best Practices

It is important to follow best practices to ensure the stability and sustainability of your IF statements. In the example below, we will show in detail the best practice method for creating IF statements.  We will also show the syntax for both Word merge field and text-based merge fields.

Example scenario: If the Opportunity Amount is greater than $10,000, show information about a complimentary service package. If it’s not, do not show anything.

We recommend the following approach:

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>10000 * 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 TrueValue. 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 FalseValue. In our example, we don’t want to display anything if the Opportunity Amount is less than $10,000, so our FalseValue 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 “«OPPORTUNITY_AMOUNT»” > “10000” “«OPPORTUNITY_SERVICE_PACKAGE»” “”}

Text-based
{IF “{{OPPORTUNITY_AMOUNT}}” > “10000” “{{OPPORTUNITY_SERVICE_PACKAGE}}”  “”}

An IF statement that is toggled open should look exactly like the examples above when it is completed.  When initially creating the IF statement, Word will include 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 will revert to the Word merge field with the placeholder name:

«IF_Amount > 10000»

Using the IF statement we just created, let’s consider an 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 are showing the value of the OPPORTUNITY_SERVICE_PACKAGE field since that is our TrueValue.

Now let’s say we use a different Opportunity called Merge Republic, which only has an amount of $8,000. Our merged document will show nothing («»), since that is our FalseValue.

Warnings

  • Checkbox fields: Although the Composer View Data Workbook and the Template Builder show the values of checkbox fields as TRUE and FALSE, those 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.
  • IF statements do not work in a Conga Email template.

Conditional logic allows you to create multiple variations of a document all in one template. Any sections/lines/paragraphs of the document can be made conditional based on the value of a field in your Lawmatics CRM. This is particularly useful for automating your documents, so that you do not need to take the time to make nit-picky changes to each client’s document, the template will simply make those changes for you automatically.

Just like you are used to with our start fresh document editor, it is simple to create DOCX documents with dynamic data, changing depending on the value of certain fields. While the start fresh editor is a bit more simple to use, creating a DOCX template is useful for more highly formatted/designed documents, since it will preserve your formatting from Word.

Before moving forward with conditional logic, make sure you are familiar with using merge fields in DOCX documents.

To build conditional logic into your document you will need to use our conditional field language in your Word document. This will be simple to use once you get the hang of it, but keep in mind you will need to get things just right in order for the logic to work properly.

Remember that all of these edits will need to take place in your Microsoft Word document before uploading it to Lawmatics.

Using IF, THEN statements

The first thing to know about setting conditional logic in a DOCX document is that you can only use true/false fields (AKA boolean) or picklist type fields to set conditional logic. This means that you can not use an open text field to set conditions. Only fields that have defined responses can be used for setting conditional logic. You also cannot use a multi-picklist field to set conditional logic.

Below is an example of how conditional logic is built in your Word templates. This example uses the field practice_area, but note that you can do conditional logic with any field.

{{#practice_area == Family Law}}We look forward to handling your Family Law matter.{{/practice_area}}{{#practice_area == Estate Planning}} We look forward to handling your Estate Planning matter.{{/practice_area}}

Let’s break this down.

You can see that all information contained inside the {{}} brackets is meant to be Lawmatics «code». Information inside these brackets will be evaluated as fields or conditions in the document.

You will need to use information inside brackets to set the IF statement for the conditional logic, then include the text of your conditional THEN statement, and lastly you will use another set of brackets to signify where the THEN statement ends.

The first line {{#practice_area == Family Law}} is what sets the first IF condition. Note that the bracketed line must be placed on its own line of text. 

The text after these brackets «We look forward to handling your Family Law matter.» is your THEN statement.

The following brackets, {{/practice_area}}, tell the system where the THEN statement ends. Notice that this text is also on its own line.

So, IF the practice area is Family Law, THEN this statement will be shown in the document. If the condition is met, then any text before the closing brackets, {{/practice_area}}, will be used.

We’ve also added a second condition to the example above. Immediately following the close of the first THEN statement, there is a second IF condition for if the practice area is estate planning, coded as such: {{#practice_area == Estate Planning}}

We then have our THEN statement for this IF condition, «We look forward to handling your Estate Planning matter.», and lastly we have closed the statement with one more closing bracket: {{/practice_area}}

So in this example, if the practice area is family law then the first sentence is shown, and if the practice area is estate planning the second sentence will be used.

This example uses the field practice_area, but note that you can create a conditional IF statement with any field.

To create an if condition you must place the condition within double brackets, {{}}. Then, inside the brackets, start with a «#» immediately followed by the field code, which again is obtained from the DOCX Field Reference mentioned above. 

You will then place a space after the field code, followed by two equal signs == and another space, and then put the value of the field that will set the IF condition. Then close the brackets.

After this opening condition, you can type all the text you would like. Then, close the IF statement with another set of double brackets, then use / immediately followed by the same field code. Not the value of the field, just the field code name.

You can create multiple if statements for the same field, enabling you to create options for each value of a field, just as demonstrated in the practice area example above.

Using IF NOT, THEN Statements

It is also possible to create an IF NOT statement. In order to accomplish this, simply use «^» in place of «#». So, for example, the following would represent text to be used if the practice area WAS NOT Estate Planning:

{{^practice_area == Estate Planning}}We look forward to handling your matter that is not Estate Planning.{{/practice_area}}
  • Hi Tom

    Thanks for the additional information :-) It’s really too bad that MS had to pull the «Custom XML» functionality out of Word, as that would probably have been the optimal approach…

    Actually, you should still be able to use it, as it is still available for
    creating
    documents. The stuff just gets pulled out the next time a document is opened in Word — but that wouldn’t matter to you.

    The advantage to the «Custom XML» is that any elements the user inserts would be in the namespace defined by the XMLSchema you attach to the document. So when you process the OpenXML, you could simply pick up the nodes for your namespace. Simple…

    Barring that, I suppose ContentControls would be the next best. In that case, I’d provide a tool for inserting the content controls that links them to a CustomXMLPart. At the same time, it would need to create the corresponding Node in the XML. That way,
    your ASP.NET application would only need to «grab» the CustomXMLPart and not need to process the entire document.xml.

    So far, so good…

    The IF processing, however, probably isn’t going to be something «out-of-the-box». Word does have IF field functionality, but it’s not as user-friendly (readable) as what people have been doing with the old tool. With Word’s internal IF field, it goes
    like this:

    1. Press Ctrl+F9 to insert a pair of field codes
    2. Enter the field code { IF «<Gender>»=»Male» «True stuff» «false stuff» }
    3. Alt+F9 to toggle off the field code
    4. F9 to force the field to update (there’s no automatic field updating

    As you can see, there are a number of factors, not least of which the proper syntax for the field code (no «Then» or «Else» — just what’s in «quotes»). And that’s probably why the old tool was set up as it was — simpler for the user to comprehend and to
    insert.

    What I have done, in the past (for mail merge) is to build a custom task pane that makes it much more intuitive for the user to create the field code. The CTP then inserts the real field code. With mail merge, however, the user will see a result for true
    or false. In your case, it’s likely to always be the «false», which could be disconcerting. Unless you keep the field codes displayed.

    Otherwise, you’ll basically need to do exactly what the old tool did. When your code encounters an <@If it will have to parse the string and do an evaluation.

    Am I helping you at all, here? I know this isn’t terribly concise, but there’s no straight-forward answer…


    Cindy Meister, VSTO/Word MVP

    • Marked as answer by
      tbau
      Tuesday, October 5, 2010 10:47 PM
    • Unmarked as answer by
      tbau
      Tuesday, October 5, 2010 10:47 PM
    • Marked as answer by
      tbau
      Tuesday, October 5, 2010 10:56 PM

  • Column A = qty
    Column F = Unit Price
    Column G = Total Amount
    

    Cell G7 contains =A7*F7. This formula works fine. However, if you copy the formula down, then you get $0.00 on lines that have no data. I tried putting in IF(A7>0,A7*F7). It returns nothing even if there is information in Cell A7 and Cell F7. Can someone help me with the correct syntax to get the value when needed and have the cell be blank when there is no item?

    asked Jun 19, 2012 at 15:52

    Bonnie Sterling's user avatar

    9

    Load 3 more related questions

    Show fewer related questions

    Like this post? Please share to your friends:
  • If statements in excel with dates
  • If statements in excel with and or not
  • If statements in excel example
  • If statements in excel cells
  • If statements for excel