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, enclose the function within <MATH> tags.
The following example uses both static numbers and numeric fields.
<MATH>(2 + 1) / 3</MATH> <MATH>( {{!Opportunity.Num1__c}} + {{!Opportunity.Num2__c}} ) / {{!Opportunity.Num3__c}}</MATH>
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.
<MATH format-number="#,###.##">{{!Opportunity.amount}} - {{!Opportunity.expectedrevenue}}</MATH>
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:
- Adding/subtracting time from a date (useful if you'd like to future date documents)
- Evaluating days/months between dates
Both types of date math require the use of the type="date" attribute within your <MATH> tag.
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):
<MATH type="date">{{!Opportunity.createdByDate}} + DAYS(X) - MONTHS(X) + YEARS(X)</math>
Evaluating Time Between Dates
To evaluate the time between two dates, use the following syntax:
<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>
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.
<MATH type="date" format-date="M/dd/yyyy">
Arithmetic Functions With Conditional Logic (RENDER Statements)
You can use arithmetic functions within your conditional logic statements. Arithmetic is processed before the value of the render statement is calculated.
<!--RENDER=((<MATH>{{!Opportunity.My_Num__c}} + {{!Opportunity.My_Num_2__c}}</MATH>) > 100) --> The sum of my numbers is more than 100! <!--ENDRENDER-->
Rounding For Conditional Logic
Arithmetic function results are evaluated by conditional logic statements without rounding, unless the format-number attribute is used to force the result to round.
For example, the following arithmetic function will evaluate to False within a conditional logic statement, since 2/3 equals 0.66666 repeating.
<math>2 / 3</math> == 0.67
Use format-number with commas for the thousands place and periods for decimal places (US number formatting) to round your arithmetic function results within conditional logic statements. For example, the following evaluates to True.
<math format-number="#,###.##">2 / 3</math> == 0.67
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.
[<math>(2 + 1) / 3</math>] [<math>( {{!Opportunity.Num1__c}} + {{!Opportunity.Num2__c}} ) / {{!Opportunity.Num3__c}}</math>]