Introduction to Conditional Logic in Document Generation
Conditional logic allows you to tailor document content based on specific data conditions in Salesforce records. Using Savvy DocGen, you can create templates that dynamically include or exclude sections, fields, or text depending on the values in your Salesforce data. This approach improves document relevance and reduces manual editing.
Why Use Conditional Logic?
- Automate document customization based on Salesforce record data.
- Reduce errors by minimizing manual updates.
- Save time by generating personalized documents quickly.
- Maintain a single template for multiple scenarios.
Types of Conditional Logic Supported
Savvy DocGen supports several ways to implement conditional logic in your templates:
- If-Else Statements: Show or hide content based on field values.
- Loops: Iterate over related records conditionally.
- Logical Operators: Combine multiple conditions using AND, OR, NOT.
Step-by-Step: Adding Conditional Logic to Your Template
1. Identify the Condition
Decide which field and value will control the conditional content. For example, you might want to show a discount clause only if the opportunity amount exceeds $50,000.
2. Use Merge Fields and Syntax
Savvy DocGen templates use a syntax similar to Salesforce merge fields combined with conditional tags. The general format for an if statement looks like this:
{% if Opportunity.Amount > 50000 %}
Discount clause text here.
{% endif %}
3. Insert Conditional Blocks in Your Template
Open your document template and place the conditional block where you want dynamic content. For example:
Dear {{Account.Name}},
{% if Opportunity.Amount > 50000 %}
We are pleased to offer you a special discount.
{% else %}
Thank you for your interest in our services.
{% endif %}
Best regards,
Sales Team
4. Test Your Conditions
Generate documents for records that meet and do not meet the condition to verify that the content displays correctly.
Advanced Conditional Logic Examples
Using Multiple Conditions
Combine conditions with logical operators:
{% if Opportunity.StageName == 'Closed Won' and Opportunity.Amount > 100000 %}
Include VIP client section.
{% endif %}
Conditional Loops Over Related Records
Loop through related records only if they exist:
{% if Account.Contacts.size > 0 %}
Contacts:
{% for contact in Account.Contacts %}
- {{contact.Name}} ({{contact.Email}})
{% endfor %}
{% endif %}
Common Pitfalls and How to Avoid Them
- Incorrect Syntax: Always close your if statements with {% endif %}. Missing tags cause errors.
- Data Type Mismatch: Compare numbers with numbers, strings with strings. Use quotes for string values.
- Field Access: Ensure the fields you reference are included in the data source for the template.
- Testing: Test with various record data to cover all logic paths.
Tips for Maintaining Conditional Templates
- Comment your logic blocks for clarity using {% comment %} Your comment {% endcomment %}.
- Keep complex logic in smaller template components if supported.
- Document the conditions and their purpose outside the template for team reference.
Conclusion
Applying conditional logic in Salesforce document generation with Savvy DocGen lets you create dynamic, personalized documents that reflect your business rules. Start by identifying key data conditions, use the supported syntax carefully, and test thoroughly to ensure your documents generate correctly every time.
Read next