About EXECLearn what the EXEC template command does and how it enables running JavaScript during document generation.Oct 14, 2025Knowledge
InformationArticle BodyOverview The EXEC template command executes JavaScript within a template to perform calculations or manipulate data during document generation. Use EXEC to augment data or compute values directly in a template without pre-processing in Salesforce. Key Functionalities Run JavaScript logic inline with your template. Define variables and functions for later use. Modify or calculate values dynamically. Syntax {#! // Your JavaScript code here#} OR {# EXEC // Your JavaScript code here#} Examples Example 1: Summing Salesforce Fields Calculate the total sum of the Amount field in Salesforce opportunities and display it as formatted currency: {#! total = 0; for(let opp of Opportunities) { total += opp.Amount; }#}Total amount of Opportunities: {# c(total) #} The output of this assuming the total amount calculates to 100,000, the local is 'en-US', and the currency code is USD would be: Total amount of Opportunities: $100,000.00 Example 2: Filtering a List Create a filtered list of opportunities where the Amount is greater than 5000: {#! filteredOpps = Account.Opportunities.filter(opp => opp.Amount > 5000);#}Filtered Opportunities: {# filteredOpps.length #} Assuming that the account has 10 Opportunities with amount greater than 5000 the output would be: Filtered Opportunities: 10 Example 3: Creating Static Variables Define variables for a number, a string, today's date, and the date one year ago. Display these variables with formatting: {#! myNum = 12345; myText = 'Your text goes here'; today = new Date(); lastYear = new Date(today.getFullYear() - 1, today.getMonth(), today.getDate());#}Number: {# n(myNum) #}Text: {# myText #}Today's Date: {# d(today) #}Last Year's Date: {# dt(lastYear) #} Assuming that today is September 9, 2023, and that the locale is 'en-US' the output of this would be: Number: 12,345Text: Your text goes hereToday's Date: 9/8/2023Last Year's Date: 9/8/2022 5:00:00 PM Example 4: Creating Custom Datasets Define an array and loop through it, then create a custom object to map Salesforce field values to custom values and dynamically access properties: {#! myArr = ['elem1', 'elem2', 'elem3']; myObj = { fieldValue1: 'your text', fieldValue2: 'other text' };#}{# FOR element IN myArr #}{# $element #}{# END-FOR element #}{# myObj[SObject.field] #}{# myObj[SObject2.field] #} Assuming that SObject.field = 'fieldValue1' and SObject2.field = 'fieldValue2', the output of this would be: elem1elem2elem3your textother text Example 5: Creating Custom Functions Define a custom function that can be called later in the template: {#! // This function returns the sum of the two passed in numbers add = (a, b) => { return a + b; };#}{# add(1, 2) #} The output of this would be: 3 Example 6: Adding Comments Add comments to your code to improve maintainability {#! // Comment explaining what the following code is doing ... myVar = { myProp: '' /* Explanation of what this property is used for */ };#} Notes JavaScript variables and functions defined within an EXEC tag are available to use anywhere in the template after the EXEC tag.The content within EXEC tags does not appear in the final merged document. Conclusion EXEC provides a flexible way to incorporate custom logic across Word, Excel, PowerPoint, and PDF templates. Next Steps EXEC in WordEXEC in ExcelEXEC in PowerPointEXEC in PDFTitleAbout EXECURL NameAbout-EXEC