Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks

Power of AMPScript Functions in SSJS

Salesforce Marketing Cloud Tips


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.Function.SetVariable("@d",d);  
    Platform.Function.SetVariable("@n",n);  
    Platform.Function.SetVariable("@f",f);  
    Platform.Function.TreatAsContent(
        "%%[SET @nd = DateAdd(@d,@n,@f)]%%"
    );
    return Platform.Function.GetVariable("@dn");
},
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 FREE e-book for you.

Sign up now to get an in-depth analysis of Adobe and Salesforce Marketing Clouds!

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

#AMPScript #automation studio #cloud page #JavaScript #programming #ssjs
Marcel Szimonisz
Marcel Szimonisz
MarTech consultant As a marketing automation consultant, I specialize in solving problems, automating processes, and driving innovation in my clients' marketing platforms.

I hold certifications in Adobe Campaign v6 (3x certified) and Salesforce Marketing Cloud (5x certified).

Additionally, I serve as a community advisor for Adobe Campaign, offering expert insights and guidance.

In addition to my professional pursuits, I also enjoy exploring various programming languages, CMSs, and frameworks, further enhancing my technical expertise and staying at the forefront of industry advancements.
Take a look at our subscription offering in case you are looking for a Marketing Automation Consultant.

Leave a comment

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

Similar posts that you may find useful

Adobe Campaign post
Adobe Campaign, Marketing Automation

ACC | Track dynamic links

3 minutes read

Some email campaign send outs can have links dynamically rendered in the content. Such links are not visible by Adobe Campaign personalization engine and thus will not be tracked. The good thing is that we can use a workaround to make dynamic links trackable and also on top of all workarounds Adobe itself has provided […]

Continue reading
Email marketing terminology
Marketing Automation

Email marketing (automation) basics

4 minutes read

Email marketing automation has become a crucial aspect of every campaign manager’s day-to-day work. Whether you’re using any email marketing automation platform, understanding the fundamentals is essential. This guide covers the basics that are shared across all platforms, ensuring you have a solid foundation in email marketing automation. Multichannel interaction Reach your recipients through various […]

Continue reading
Adobe Campaign Classic tips
ACC Tips & Tricks, Adobe Campaign

Reselect temporary schema in query

less than a minute read

When working with the Query activity in Adobe Campaign Classic, you have the choice to select either the Temporary schema or the Database schema. If you opt for the Temporary schema, it is essential to reselect it even if it is preselected with a default value. Here’s why it’s important to explicitly reselect the Temporary […]

Continue reading
Adobe Campaign post
Adobe Campaign, Marketing Automation

ACC | All about variables in Adobe Campaign

2 minutes read

In this article I will explain all sorts of variables you can come across when working with the tool. Also I will discuss different less known approach of storing variables. Workflow variables In adobe campaign we have way how to store variables between consecutive activities. Normally you would store variables as instance or context variables. […]

Continue reading
Mastering Marketing Automation: From Data to Engagement
Marketing Automation

Mastering Marketing Automation: A Guide

6 minutes read

Marketing automation is a technology-driven strategy that streamlines, automates, and measures marketing tasks and workflows. It aims to enhance efficiency, personalize customer experiences, and drive results by utilizing software platforms and technologies to automate repetitive tasks and nurture leads through the sales funnel. Marketing automation is a multifaceted process comprised of four essential steps. First […]

Continue reading