Custom Embedded JavaScript Functions: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 13: Line 13:
In order to add custom JavaScript function, you need to create a Function Expression record, which you can access from the 3B Docs Application (or simply search for “Function Expressions” in the app launcher drop down).
In order to add custom JavaScript function, you need to create a Function Expression record, which you can access from the 3B Docs Application (or simply search for “Function Expressions” in the app launcher drop down).


When you have navigated to the object list view, you can click on the “New” button which will display the new record form. What is important here is that you give the Function Expression a unique name that has no spaces or special characters. Don’t worry about enforcing this rule yourself, we have built a salesforce validation rule to make sure that the name of the Value Formatter is compliant with 3B Docs.  
When you have navigated to the object list view, you can click on the “New” button which will display the new record form. What is important here is that you give the Function Expression a unique name that has no spaces or special characters. Don’t worry about enforcing this rule yourself, we have built a salesforce validation rule to make sure that the name of the [[Custom Value Formatting|Value Formatter]] is compliant with 3B Docs.  


To use the function expression in your template, navigate to the “Functions” icon within the Template builder.
To use the function expression in your template, navigate to the “Functions” icon within the Template builder.
Line 26: Line 26:


== Syntax ==
== Syntax ==
Just like custom value formatters, when you create a Function Expression, you are basically creating a JavaScript function. This JavaScript function has access to the global context and thus to the user’s navigator object, the application variables & collections and much more.
Just like [[Custom Value Formatting|custom value formatters]], when you create a Function Expression, you are basically creating a JavaScript function. This JavaScript function has access to the global context and thus to the user’s navigator object, the application variables & collections and much more.


The difference between custom value formatters and Function Expressions lies in the fact that custom value formatters can only be applied to a specific [[Merge Fields and Tags|merge tag]], whereas [[Custom Embedded JavaScript Functions|Function Expressions]] form their own independent merge tags.
The difference between [[Custom Value Formatting|custom value formatters]] and Function Expressions lies in the fact that [[Custom Value Formatting|custom value formatters]] can only be applied to a specific [[Merge Fields and Tags|merge tag]], whereas [[Custom Embedded JavaScript Functions|Function Expressions]] form their own independent [[Merge Fields and Tags|merge tags]].


You must always return a string value – this return value will be your “transformation” of the merge tag.  
You must always return a string value – this return value will be your “transformation” of the [[Merge Fields and Tags|merge tag]].  


== Examples ==
== Examples ==
Line 36: Line 36:


=== Return Current Date ===
=== Return Current Date ===
This example is useful for getting a dynamically generated timestamp of the current date relative to when the document is opened in dd/mm/yyyy format (don’t worry, you can still apply custom value formatters and transform the date to a US date!)<syntaxhighlight lang="javascript" line="1">
This example is useful for getting a dynamically generated timestamp of the current date relative to when the document is opened in dd/mm/yyyy format (don’t worry, you can still apply [[Custom Value Formatting|custom value formatters]] and transform the date to a US date!)<syntaxhighlight lang="javascript" line="1">
() => new Date().toLocaleDateString(‘en-GB’);
() => new Date().toLocaleDateString(‘en-GB’);
</syntaxhighlight>
</syntaxhighlight>