Work with the JavaScript Editor
Use the JavaScript editor to add custom JavaScript functions that meet your business requirements, for example, a JavaScript function to validate whether required fields in a form have values.
Any JavaScript code that you add will have a defined scope based on the editor where you write the code. If your code will only be called from within a specific page (for example, to load some data when the page loads), you can write your code in the page-level JavaScript editor. If you want a JavaScript function to be used on multiple pages (for example, to load libraries for customizing navigation elements or custom web components), then you'll need to use the JavaScript editor for the flow or the application. You can also define JavaScript functions at the layout and fragment level.
The JavaScript editor displays a particular artifact's JS file. So if you open the main
flow's JavaScript editor (for example), you're seeing the contents of the main-flow.js
file. Each artifact has its own JS file: an application artifact uses the app-flow.js
file, a flow uses flow-name-flow.js
, and a page uses page-name-page.js
. A layout uses layout.js
and a fragment uses fragment-name-fragment.js
.
The JavaScript editor provides extensive suggestions as you enter text to provide code-completion capabilities. This includes code snippets for common JavaScript structures, such as "for" and "while" loops and conditional statements. For example, typing for in the JavaScript editor will show you various "for" loop structures:
Description of the illustration js-code-complete-example.png
Selecting a structure will let you easily switch the variables in the structure.
Add a Custom JavaScript Function
To add a custom JavaScript function, you define the function within the class provided in the JavaScript editor for your page, flow, or application. You can also add JavaScript functions to layouts and fragments.
- In an action chain, use the Call Function action. See Add a Call Function Action.
- In a component's Properties pane, select the function in the Expression editor or Variables picker of a property:
To write efficient expressions that handle situations where a referenced field might not be available or the field's value could be null, see How Do I Write Expressions If a Referenced Field Might Not Be Available Or Its Value Could Be Null?
Use RequireJS to Reference External JavaScript Files
If you want to use RequireJS to refer to external JavaScript libraries in your application, you can add a requirejs
statement to your application's definition, then import the library.
Use Variables with a JavaScript Module
You can't directly get or set variables from within your JavaScript modules. However, you can use the Call Module Function action to access your JS module. This action takes an array of parameters which can include variables and can return a result that you can assign to a variable.
This approach ensures that the variable has a consistent state from the beginning to the end of your action chain's execution.
To "get" a value, pass the variable in as a parameter to the module function that you are calling using a callModuleFunction
action in the action chain.
To "set" a variable based on the return value from that callModuleFunction
, use an Assign Variables action to copy the result of the function into the desired variable in whatever scope.