For the complete documentation index, see llms.txt. This page is also available as Markdown.

Setup Calculations

Learn the basic math tag patterns for numbers, dates, conditions, and null-safe output.

What You'll Learn

In this tutorial, you'll build a quote summary that uses calculation tags to produce clean numeric and date output. By the end, you'll know how to:

  • Calculate a deposit amount with <MATH>

  • Calculate a remaining balance from the same source field

  • Add days to a date with type="date"

  • Use a calculation result inside conditional logic

  • Prevent blank numeric fields from causing errors

What You'll Build

An Opportunity quote summary that includes:

  • The opportunity amount

  • A 10% deposit amount

  • A remaining balance

  • A follow-up date 30 days after close

  • A manager review note for larger deals

Estimated time: 15 minutes

Prerequisites

Before starting, ensure you have:

  • Access to S-Docs Templates in Salesforce

  • Permission to create or edit templates

  • An S-Docs template related to Opportunity

  • A test opportunity with values in Amount and CloseDate

  • Basic familiarity with merge fields such as {{!Opportunity.Amount}}

If you are brand new to S-Docs syntax, complete a basic merge field tutorial first.

If you insert fields with the field picker, remove any auto-added number or date formatting before using those fields inside <MATH> tags.

1

Step 1: Open or Create Your Template

Start with an Opportunity template where you want to show calculated values.

  1. Navigate to S-Docs Templates in Salesforce

  2. Open an existing template or click New Template

  3. Choose Opportunity as the related object

  4. Open the template editor

  5. Click Source if you want to paste the syntax directly

You now have a place to add calculation tags.

2

Step 2: Add Your First Calculation

Add a quote summary section with a 10% deposit calculation.

What you just did:

  • Used <MATH> to multiply the opportunity amount by 0.10

  • Added format-number="#,###.00" for commas and two decimals

  • Kept the original field visible for comparison

3

Step 3: Add the Remaining Balance

Now calculate the amount left after the deposit.

What you just did:

  • Subtracted the deposit from the full amount

  • Used parentheses so the deposit is calculated first

  • Kept spaces around operators so S-Docs reads the math correctly

4

Step 4: Add a Date Calculation

Use a date calculation when you need a due date, reminder date, or follow-up date.

What you just did:

  • Started the expression with a date field

  • Added type="date" so S-Docs treats the expression as date arithmetic

  • Formatted the result as a readable date like April 23, 2026

5

Step 5: Use a Calculation Inside Conditional Logic

You can evaluate a calculated result before showing a message.

Add this block below the quote summary:

What you just did:

  • Calculated the remaining balance inside the condition

  • Compared that result to 10000

  • Rendered the note only when the threshold is passed

6

Step 6: Make the Calculation Safe for Blank Values

Blank numeric fields can cause Invalid double and related errors.

Use replaceall to substitute a blank value with 0.

What you just did:

  • Replaced a blank amount with 0

  • Protected the math expression from null-style numeric errors

  • Created a pattern you can reuse with optional numeric fields

7

Step 7: Save and Test Your Template

Now test the calculation output with a real opportunity.

  1. Click Save in the template editor

  2. Generate the template from an opportunity with Amount and CloseDate

  3. Check the deposit, remaining balance, and follow-up date

  4. Test again with a larger amount to trigger the manager review note

What to verify:

  • The deposit shows 10% of the opportunity amount

  • The remaining balance equals the amount minus the deposit

  • The follow-up date is 30 days after close

  • The manager review note appears only when the balance is above 10000

8

Step 8: Try a DOCX Variation

If your template format is DOCX, wrap the full math expression in square brackets.

What changed:

  • The calculation uses bracketed Microsoft template syntax

  • The full math block stays inside the brackets

  • The same formula logic still applies

Common Issues and Solutions

Issue 1: "The result does not show commas or decimals"

Problem: The number calculates, but the output is not formatted.

Solution:

  • Add format-number on the <MATH> tag itself

  • Use a pattern like #,###.00

  • Regenerate the document after saving the change

Issue 2: "I get an Invalid double error"

Problem: One of the numeric fields in the expression is blank.

Solution:

  • Replace blank values with zero using replaceall=" ,0"

  • Test the source record to confirm the numeric field has data

  • Apply the same null-safe pattern to every optional numeric field in the calculation

Issue 3: "Date math fails with a date format error"

Problem: S-Docs does not recognize the first value in the expression as a date.

Solution:

  • Make sure the expression uses <MATH type="date">

  • Start the expression with a date field such as {{!Opportunity.CloseDate}}

  • If you use a static value, format it as yyyy-MM-dd

Issue 4: "The subtraction result looks wrong"

Problem: The total does not match the expected deposit or balance.

Solution:

  • Add parentheses around the inner calculation

  • Keep spaces around operators like +, -, *, and /

  • Test the deposit formula by itself before combining it with a second expression

Issue 5: "The manager review note never appears"

Problem: The conditional block stays hidden even for large deals.

Solution:

  • Check the threshold value in the RENDER condition

  • Confirm the calculation returns a number before the comparison

  • Test the same calculation outside the conditional block first

What You've Learned

Congratulations! You've built a template with calculation tags and learned:

✅ How to calculate a percentage with <MATH> ✅ How to calculate a remaining balance from one field ✅ How to add days to a date with type="date" ✅ How to use a math result inside RENDER logic ✅ How to prevent blank-value errors with replaceall

Next Steps

Now that you've mastered the basics, you can:

Expand Your Calculations

  • Add tax, subtotal, or discount calculations

  • Build date offsets for reminders and due dates

  • Reuse the same calculation pattern in other template sections

Combine with Other Features

Practice More

  • Add a late-fee calculation to an invoice template

  • Show approval language only above a calculated threshold

  • Build due-date logic for contract follow-up steps

Practice Exercise

To reinforce what you've learned, build an invoice-style Opportunity template that includes:

  1. A subtotal based on Opportunity.Amount

  2. A 7% tax calculation

  3. A final total that adds subtotal and tax

  4. A payment due date 14 days after CloseDate

  5. An approval note that appears only when the final total is greater than 25000

Bonus challenges:

  • Make every numeric field null-safe

  • Add a second threshold message for totals above 50000

  • Convert one calculation to DOCX syntax

Last updated

Was this helpful?