Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks

SFMC TIP | How to use 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 #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 Classic tips
ACC Tips & Tricks, Adobe Campaign, Marketing Automation

ACC TIP | How to copy query conditions

1 minute read

Perhaps you’ve encountered an unusual occurrence while attempting to copy complex query conditions to your clipboard. In some instances, only a portion of the content was copied, while in others, nothing was copied at all. I’ve personally faced similar situations over the years but never delved deeper into the matter. However, one day, I reached … Read more

Continue reading
SFMC tips and tricks
Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks

SFMC TIP | Invalid links in HTML

1 minute read

When the HTML template is used (loading your own HTML code) all the links that use query parameters (?a=b) will resolve to invalid links when they redirect upon user click. This happens when web analytics UTM tags are being attached to the delivery. To resolve this issue all the links with additional query parameters has … Read more

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

ACC TIP | Fast select field in query builder

less than a minute read

Working with Adobe Campaign Classic often involves frequent mouse movements and clicks. However, after a while, you’ll discover certain tricks to work with this tool more efficiently and quickly. Today, I’ll show you a simple trick that will help you enter fields in the query builder faster and without having to move your mouse across … Read more

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

ACC TIPS | 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 … Read more

Continue reading
Adobe Campaign post
Adobe Campaign, Marketing Automation

ACC | Add action button to the form view

2 minutes read

Have you ever wondered how to add new button with custom functionality to form view. I will show you step by step how to do it. In my example I will create signatures used in email campaigns, which will be dependent on the recipient’s profile information such as language, country etc. The way how I … Read more

Continue reading