Power of AMPScript Functions in SSJS
Have you ever wondered, while working with SSJS, if there was a function in SSJS similar to the one you use in AMPscript? What if I told you that you can bring native AMPscript functions to SSJS? Today, I will show you how to incorporate the best features from both scripting worlds into SSJS automation, emails, or cloud pages.
To connect the two different scripting language worlds, we will utilize the TreatAsContent
function, which is used to execute the AMPscript block and retrieve the output.
<script runat="server"> //language="javascript"> if not set it defaults to JavaScript var dateAdd = function(d,n,f){ Platform.Variable.SetValue("@d",d); Platform.Variable.SetValue("@n",n); Platform.Variable.SetValue("@f",f); Platform.Variable.TreatAsContent( "\%\%[SET @fd = DateAdd(@d,@n,@f)]%%" ); return Platform.Variable.GetValue("@fd"); }, minus5days = dateAdd(new Date(), -5,"D") // Valid values for third argument // include Y, M, D, H, and MI (minutes) </script>
- The
<script runat="server">
tag indicates that the code is meant to be executed on the server-side. var dateAdd = function(d, n, f) { ... }
defines a JavaScript function calleddateAdd
that takes three parameters:d
,n
, andf
. This function is responsible for adding a specific duration (n
) to a given date (d
) based on the provided format (f
).Platform.Function.SetVariable("@d",d);
sets an AMPscript variable@d
with the value of the JavaScriptd
parameter.Platform.Function.SetVariable("@n",n);
sets an AMPscript variable@n
with the value of the JavaScriptn
parameter.Platform.Function.SetVariable("@f",f);
sets an AMPscript variable@f
with the value of the JavaScriptf
parameter.Platform.Function.TreatAsContent("%%[SET @nd = DateAdd(@d,@n,@f)]%%");
executes an AMPscript block within the SSJS function. It usesSET
to assign a new variable@nd
with the result of theDateAdd
function, which adds the provided duration (@n
) to the given date (@d
).return Platform.Function.GetVariable("@dn");
retrieves the value of the@dn
AMPscript variable, which was set in the previous step, and returns it as the result of thedateAdd
function.minus5days = dateAdd(new Date(), -5,"D")
calls thedateAdd
function with the current date (obtained usingnew Date()
), a negative value of 5, and the format'D'
(representing days). It assigns the result to the variableminus5days
.
<script runat="server"> //language="javascript"> if not set it defaults to JavaScript var dateAdd = function(d,n,f){ Platform.Function.SetVariable("@d",d); Platform.Function.SetVariable("@n",n); Platform.Function.SetVariable("@f",f); return Platform.Function.TreatAsContent( "%%[Output(DateAdd(@d,@n,@f))]%%" ); }, minus5days = dateAdd(new Date(), -5,"D") // Valid values for third argument // include Y, M, D, H, and MI (minutes) </script>
- The
<script runat="server">
tag indicates that the code is meant to be executed on the server-side. var dateAdd = function(d, n, f) { ... }
defines a JavaScript function calleddateAdd
that takes three parameters:d
,n
, andf
. This function is responsible for adding a specific duration (n
) to a given date (d
) based on the provided format (f
).Platform.Function.SetVariable("@d",d);
sets an AMPscript variable@d
with the value of the JavaScriptd
parameter.Platform.Function.SetVariable("@n",n);
sets an AMPscript variable@n
with the value of the JavaScriptn
parameter.Platform.Function.SetVariable("@f",f);
sets an AMPscript variable@f
with the value of the JavaScriptf
parameter.return Platform.Function.TreatAsContent("%%[Output(DateAdd(@d,@n,@f))]%%");
executes an AMPscript block within the SSJS function. It usesDateAdd
function to add the provided duration (@n
) to the given date (@d
) based on the specified format (@f
). TheOutput
function is used to display the result.minus5days = dateAdd(new Date(), -5,"D")
calls thedateAdd
function with the current date (obtained usingnew Date()
), a negative value of 5, and the format'D'
(representing days). The result of thedateAdd
function is assigned to theminus5days
variable.