Expressions are a powerful addition to Maven Documents. You've probably already noticed this term when going through elements reference. Some parameters have an option to use Expressions as the value.
Maven Documents Expressions use JavaScript Expression Language. It is a powerful context-based expression parser and evaluator. Examples from the Jexl Documentation are fully compatible with Maven Documents.
There is also a free Jexl Playground that you can explore and play around in.
You can copy-paste native Jexl expressions to your document mind the two following things:
- Expressions need to have a certain form for Maven Documents to recognize them successfully.
They are written in the following notation:{!$Expression}
.- If
{!$Expression}
receives any arguments, you need to make sure those arguments exist and are valid.
Arguments from the Jexl Documentation are most certainly not valid for you specific case.
We prepared some examples that you can test in your environment.
We suggest you use the Sales Quote from our Template Gallery. This template is fully compatible with all the functions we use later in the article. These functions are sorted by the input (argument) they ingest and you can find them below.
We also built custom Expressions based on feedback from our current clients who are using Maven Documents. These custom Expressions proved to be quite helpful in making a complex Document Template and serve as enrichment to standard Jexl ones.
They are marked as "custom" in tables below.
You can use Expressions anywhere in the template and in Maven Documents:
{!$Expression}
notation.Every field value inside of an expression is considered a String at first, regardless of its data type in Salesforce. It means that it's important to wrap the field value with the corresponding function when performing data type-specific operations.
For example, one might think that the result of the following expression: {!$Opportunity[0].Amount + 10}
is the sum of the first opportunity's amount and 10. That is incorrect. As the "Opportunity Amount" field value is considered a String, the expression parser validates this as a concatenation of two string values.
To perform addition we need to wrap the "Opportunity Amount" field value using the toDecimal
function.
The same applied to date fields. To show the correct value of "date" and "dateTime" fields in expressions, you need:
{!$format(date(Opportunity[0].CloseDate), 'date', 'short')}
.{!$format(dateTime(Opportunity[0].CreatedDate), 'dateTime', 'medium')}
.Expression | Expected | Actual |
---|---|---|
{!$count( |
Returns the amount of "Account" records. | 1 |
{!$isEmpty( |
Checks if the "Account" Object has no records. | false |
{!$isNotEmpty( |
Checks if the "Account" Object has records. | true |
{!$max(Account[0] |
Returns the largest "Account Opportunity" amount. | 435000 |
{!$min(Account[0] |
Returns the smallest "Account Opportunity" amount. | 435000 |
{!$sum(Account[0] |
Returns the sum of "Account Opportunity" amounts. | 435000 |
{!$join(Account[0] |
Returns an array of items with a defined separator between. | 1, 2, 1, 1 |
Expression | Expected | Actual |
---|---|---|
{!$isBlank(Account[0] |
Checks if the "Account" record's "Name" field is empty. | false |
{!$contains(Account[0] |
Checks if the "Account" record's "Name" field contains the letter "a". | true |
{!$charAt(Account[0].Name, 0)} |
Returns the first letter of the value in the "Account Name" field. | E |
{!$charCodeAt(Account[0].Name, 2)} |
Returns an integer between 0 and 65535 representing the UTF-16 code unit of the defined letter from the value of the "Account Name" field. | 83 |
{!$concat('The ', 'name ', |
Concatenates the provided string arguments and returns a new string. | The name of the Opportunity is Edge SLA |
{!$endsWith(Opportunity[0].Name, 'SLA', |
Determines whether the "Opportunity" record's name ends with the characters of the defined string. The last "Position" parameter where the string is expected to be found is optional. | true |
{!$indexOf(Account[0].Name, 'a', |
Returns the position of the first "a" letter in the name** of the "Account" record. The last "Position" parameter where the letter is expected to be found is optional. | 12 |
{!$lastIndexOf(Account[0] |
Returns the position of the last "a" letter in the name of the "Account" record. | 34 |
{!$repeat(Opportunity[0].Name, 2)} |
Builds and returns a new string that contains the defined number of copies of the "Opportunity" record's name concatenated together. | Edge SLAEdge SLA |
{!$slice(Opportunity[0].Name, 3, 7)} |
Extracts a part of the defined "Opportunity" record's name and returns it as a new string. | e SL |
{!$split(Account[0].Name, 'i', 3)} |
Divides the defined "Account" record's name into an ordered list of substrings by searching for the pattern. Composes and returns the array of substrings. The last "Limit" parameter stands for the number of substrings and is optional. | "Edge Commun", "cat", "ons" |
{!$startsWith(Opportunity[0].Name, 'E', |
Determines whether the defined "Opportunity" record's name begins with E. The last "Position" parameter where the letter is expected to be found is optional. | true |
{!$substring(Account[0] |
Returns the part of the Account Name between the 0th and 4th Indexes. | Edge |
{!$toLowerCase(Opportunity[0].Name)} |
Returns the defined "Opportunity" record's name in lower case letters. | edge sla |
{!$toUpperCase(Opportunity[0].Name)} |
Returns the defined "Opportunity" record's name in upper case letters. | EDGE SLA |
{!$trim( Account[0].Name )} |
Removes whitespaces from both ends of the defined "Account" record's name. | Edge Communications |
{!$replace(Account[0].Name, 'i', 'EE', 'flags')} |
Returns a new string with the first found "i" letter replaced by "EE" in the defined "Account" record's name. If you use the "flags" parameter, the second argument is interpreted as the regular expression. The "flags" parameter is optional. | Edge CommunEEcations |
{!$replaceAll(Field, 'pattern', 'replacement', 'flags')} |
Returns a new string with all "i" letters replaced by "EE" in the defined "Account" record's name. If you use the "flags" parameter, the second argument is interpreted as the regular expression. The "flags" parameter is optional. | Edge CommunEEcatEEons |
Expression | Expected | Actual |
---|---|---|
{!$toDecimal(Account[0] |
Returns the amount of the defined "Opportunity" record. | 435000 |
{!$toDecimal(Account[0] |
Returns the sum of the amount of the defined "Opportunity" record and 10. | 435010 |
{!$toPercent(0.123, |
Shows the number as the percentage with 2 digits after comma. | 12.30% |
{!$round(10.234, |
Rounds the number to 2 digits after comma. | 10.23 |
{!$setScale(10.23, |
Shows the number with 4 digits after comma. | 10.2300 |
{!$row()} |
Returns the row number of the Cell from Repeater in Google Sheets or XLSX file. | 12 |
{!$column()} |
Returns the column letter of the Cell from Repeater in Google Sheets or XLSX file. | E |
{!$index()} |
Returns the index (ordinal number) of the repeating value in Repeater and Table. | 1, 2, 3 |
Expression | Expected | Actual |
---|---|---|
{!$date(Account[0] |
Returns the full Opportunity's closure date and time. It should be wrapped in the "format" function. | Sat Jun 05 2021 00:00:00 GMT-0400 (Eastern Daylight Time) |
{!$time(Account[0] |
Returns the full Opportunity's closure time. It should be wrapped in the "format" function. | 00:00:00 GMT-0400 (Eastern Daylight Time) |
| {!dateTime(
| Returns the **full** Account's **creation date** and **time**. It should be wrapped in the "format" function. | Wed Aug 04 2021 04:37:00 GMT-0400 (Eastern Daylight Time) |
| {!now()}
| Returns the full current date and time. It should be wrapped in the "format" function. | Tue Aug 24 2021 03:10:24 GMT-0400 (Eastern Daylight Time) |
| {!addDays(now(),
| Returns the **full current date** and **time + 10 days**. It should be wrapped in the "format" function. | Fri Sep 03 2021 03:10:24 GMT-0400 (Eastern Daylight Time) |
| {!addMonths(now(),
| Returns the full current date and time + 10 months. It should be wrapped in the "format" function. | Fri Jun 24 2022 03:10:24 GMT-0400 (Eastern Daylight Time) |
| {!$addYears(now(),
| Returns the full current date and time + 10 years. It should be wrapped in the "format" function. | Sun Aug 24 2031 03:10:24 GMT-0400 (Eastern Daylight Time) |
Expression | Expected | Actual |
---|---|---|
{!$format( |
Opportunity Amount with two decimal 00 | 435,000.00 |
{!$format(now(), |
Returns the current date in the short format. | 8/24/2021 |
{!$format(now(), |
Returns the current time in the short format. | 12:10 AM |
{!$format(now(), |
Returns the current date and time in the short format. | 8/24/2021, 12:10 AM |
{!$format(dateTime |
Returns the date when the "Opportunity" records were last modified in the short format. | 8/24/2021 |
{!$format(date(Account[0]. |
Returns the date value of the defined field in a custom format. | 2021-08-03 |
{!$format(dateTime |
Returns the date and time when the "Opportunity" record were created in a custom format. | 2021-08-24/17:21:00 |
Be careful with year formats: yyyy represents the calendar year of the date, while YYYY represents the calendar year of the week.
For example, the calendar year of 30/12/2024 is 2024. But technically, the 30th of December falls in the first week of 2025. The calendar year for the week is 2025.
Hence, yyyy and YYYY represent different years for the same date or timestamp.
Lets say that {!$toDecimal(Account[0].Opportunities[0].Amount)} gives value of 150.
Expression | Expected | Actual |
---|---|---|
{!$toDecimal( |
Checks wheteher the Account opportunity's amount is less than 100. | false |
{!$toDecimal( |
Checks wheteher the Account opportunity's amount is greater than 100. | true |
{!$toDecimal( |
Checks wheteher the Account opportunity's amount is lesser than or equal to 100. | false |
{!$toDecimal( |
Checks wheteher the Account opportunity's amount is greater than or equal to 100 | true |
Expression | Expected | Actual |
---|---|---|
{!$toBoolean( |
Checks whether the Account opportunity's amount is less than 100. If it is, the expression returns "true". Otherwise, it returns "false". | false |