Inline Function in Dynamic Action JavaScript Expression in APEX

In some Dynamic Actions (DA) settings in APEX you have the option to use a JavaScript (JS) Expression. A common example of this is setting a value where the Set Type is JavaScript:

As the name (JavaScript Expression) suggests this should be an expression such as 1 + 2; If you try to run multiple lines of code with a return statement (as shown above) the following error is raised:

1
Uncaught TypeError: apex.da.initDaEventList is not a function

To resolve this issue you can use an immediately-invoked Function Expressions (IIFE) as the JavasScript Expression. The following example re-writes the code from the first example as an IIFE and will work as a valid JavaScript Expression in the DA:

1
2
3
4
(function() {
var today = new Date();
return today.getFullYear();
})()

Thanks to Adrian Png and Trent Schafer for the help on this.