🔥 500+ people already subscribed. Why not you? Get our newsletter with handy code snippets, tips, and marketing automation insights.

background shape
background shape

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>
  1. The <script runat="server"> tag indicates that the code is meant to be executed on the server-side.
  2. var dateAdd = function(d, n, f) { ... } defines a JavaScript function called dateAdd that takes three parameters: d, n, and f. This function is responsible for adding a specific duration (n) to a given date (d) based on the provided format (f).
  3. Platform.Function.SetVariable("@d",d); sets an AMPscript variable @d with the value of the JavaScript d parameter.
  4. Platform.Function.SetVariable("@n",n); sets an AMPscript variable @n with the value of the JavaScript n parameter.
  5. Platform.Function.SetVariable("@f",f); sets an AMPscript variable @f with the value of the JavaScript f parameter.
  6. Platform.Function.TreatAsContent("%%[SET @nd = DateAdd(@d,@n,@f)]%%"); executes an AMPscript block within the SSJS function. It uses SET to assign a new variable @nd with the result of the DateAdd function, which adds the provided duration (@n) to the given date (@d).
  7. 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 the dateAdd function.
  8. minus5days = dateAdd(new Date(), -5,"D") calls the dateAdd function with the current date (obtained using new Date()), a negative value of 5, and the format 'D' (representing days). It assigns the result to the variable minus5days.
<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>
  1. The <script runat="server"> tag indicates that the code is meant to be executed on the server-side.
  2. var dateAdd = function(d, n, f) { ... } defines a JavaScript function called dateAdd that takes three parameters: d, n, and f. This function is responsible for adding a specific duration (n) to a given date (d) based on the provided format (f).
  3. Platform.Function.SetVariable("@d",d); sets an AMPscript variable @d with the value of the JavaScript d parameter.
  4. Platform.Function.SetVariable("@n",n); sets an AMPscript variable @n with the value of the JavaScript n parameter.
  5. Platform.Function.SetVariable("@f",f); sets an AMPscript variable @f with the value of the JavaScript f parameter.
  6. return Platform.Function.TreatAsContent("%%[Output(DateAdd(@d,@n,@f))]%%"); executes an AMPscript block within the SSJS function. It uses DateAdd function to add the provided duration (@n) to the given date (@d) based on the specified format (@f). The Output function is used to display the result.
  7. minus5days = dateAdd(new Date(), -5,"D") calls the dateAdd function with the current date (obtained using new Date()), a negative value of 5, and the format 'D' (representing days). The result of the dateAdd function is assigned to the minus5days variable.

Oh hi there 👋
I have a SSJS skill for you.

Sign up now to get an SSJS skill that can be used with your AI companion

We don’t spam! Read our privacy policy for more info.

Share With Others

The Author
Marcel Szimonisz

Marcel Szimonisz

MarTech consultant

I specialize in solving problems, automating processes, and driving innovation through major marketing automation platforms, particularly Salesforce Marketing Cloud and Adobe Campaign.

Your email address will not be published. Required fields are marked *

Buy me a coffee
Subscribe

Get exclusive tips, scripts and news

Choose your topics

We don’t spam! Read our privacy policy for more info.

Similar posts