How to use 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 calleddateAddthat 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@dwith the value of the JavaScriptdparameter.Platform.Function.SetVariable("@n",n);sets an AMPscript variable@nwith the value of the JavaScriptnparameter.Platform.Function.SetVariable("@f",f);sets an AMPscript variable@fwith the value of the JavaScriptfparameter.Platform.Function.TreatAsContent("%%[SET @nd = DateAdd(@d,@n,@f)]%%");executes an AMPscript block within the SSJS function. It usesSETto assign a new variable@ndwith the result of theDateAddfunction, which adds the provided duration (@n) to the given date (@d).return Platform.Function.GetVariable("@dn");retrieves the value of the@dnAMPscript variable, which was set in the previous step, and returns it as the result of thedateAddfunction.minus5days = dateAdd(new Date(), -5,"D")calls thedateAddfunction 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 calleddateAddthat 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@dwith the value of the JavaScriptdparameter.Platform.Function.SetVariable("@n",n);sets an AMPscript variable@nwith the value of the JavaScriptnparameter.Platform.Function.SetVariable("@f",f);sets an AMPscript variable@fwith the value of the JavaScriptfparameter.return Platform.Function.TreatAsContent("%%[Output(DateAdd(@d,@n,@f))]%%");executes an AMPscript block within the SSJS function. It usesDateAddfunction to add the provided duration (@n) to the given date (@d) based on the specified format (@f). TheOutputfunction is used to display the result.minus5days = dateAdd(new Date(), -5,"D")calls thedateAddfunction with the current date (obtained usingnew Date()), a negative value of 5, and the format'D'(representing days). The result of thedateAddfunction is assigned to theminus5daysvariable.






