Arithmetic Function Syntax

S-Docs templates support arithmetic functions for both static numbers and numeric fields. All functions should be written using standard Salesforce math operators. S-Docs will evaluate all functions using the standard mathematical order of operations.

To use arithmetic functions in your S-Docs templates, simply enclose the function within <MATH> tags.

The following example uses both static numbers and numeric fields.

[code lang="html"]<MATH>(2 + 1) / 3</MATH>

<MATH>( {{!Opportunity.Num1__c}} + {{!Opportunity.Num2__c}} ) / {{!Opportunity.Num3__c}}</MATH>[/code]

Note: You can use negative numbers in your functions. Keep in mind that subtraction functions should include a space on either side of the minus symbol to differentiate between negative numbers and subtraction functions. For example, to evaluate three minus two, use the following syntax:

[code lang="html"]<MATH>3 - 2</MATH>[/code]

Instead of something like this:

[code lang="html"]<MATH>3 -2</MATH>[/code]

Formatting Numbers

To format the result of your arithmetic function, use the format-number attribute within the <MATH> tag as shown below, but not within the merge fields.

[code lang="html"]<MATH format-number="#,###.##">{{!Opportunity.amount}} - {{!Opportunity.expectedrevenue}}</MATH>[/code]

Note: When you insert numeric fields using the Insert Field button, number formatting is added to the merge fields automatically. Ensure that you remove this formatting when using merge fields within arithmetic functions.

Arithmetic Functions with Date Fields

You can also use date fields or static dates within your arithmetic functions. The two main types of date math supported are:

  1. Adding/subtracting time from a date
  2. Evaluating days/months between dates

Both types of date math require the use of the type="date" attribute within your <MATH> tag.

Note: Static dates must be written in the yyyy-MM-dd format (for example, January 1, 2021 would look like 2021-01-01).

Adding/Subtracting Time

To add or subtract days, months, or years from any date field, use the following syntax (where X equals the number of days, months, or years you would like to add or subtract from the value of the date field):

[code lang="html"]<MATH type="date">{{!Opportunity.createdByDate}} + DAYS(X) - MONTHS(X) + YEARS(X)</math>[/code]

Evaluating Time Between Dates

To evaluate the time between two dates, use the following syntax:

[code lang="html"]<MATH type="date">DaysBetween({{!Opportunity.createdByDate}},{{!Opportunity.closedate}}</MATH>
<MATH type="date">MonthsBetween({{!Opportunity.createdByDate}},{{!Opportunity.closedate}}</MATH>
<MATH type="date">YearsBetween({{!Opportunity.createdByDate}},{{!Opportunity.closedate}}</MATH>[/code]

Formatting Dates

To format the result of your arithmetic date function, use the format-date attribute within the <MATH> tag as shown below, but not within the merge fields.

[code lang="html"]<MATH type="date" format-date="M/dd/yyyy">[/code]

Note: When you insert date fields using the Insert Field button, date formatting is added to the merge fields automatically. Ensure that you remove this formatting when using date fields within arithmetic functions.

DOCX Syntax

When using arithmetic functions in DOCX templates, use lowercase <math> tags and be sure to enclose the entire function within square brackets, as shown in the following example.

[code lang="html"][<math>(2 + 1) / 3</math>]
[<math>( {{!Opportunity.Num1__c}} + {{!Opportunity.Num2__c}} ) / {{!Opportunity.Num3__c}}</math>][/code]

Top