SFMC TIP | How to use AMPScript functions in SSJS

Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks
3 minutes read


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.
SFMC tips and tricks
Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks

SFMC TIP | The Power of Send Logging

1 minute read

Send logging in Salesforce Marketing Cloud is a feature that allows you to track the delivery, open, click, and other engagement metrics for the emails that you send from your account. This feature helps you to gain insights into the performance of your email campaigns and optimize them for better results. When you enable send … Read more

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

SFMC TIP | How to Link Journey to Delivery

1 minute read

In Salesforce Marketing Cloud, you can link the customer journey to delivery using data views. Custom views that contain e.g. all tracking information. Here are some steps you can follow to link the journey to delivery using data views: This SQL view will return data such as the subscriber key, event date, journey and activity … Read more

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

SFMC TIP | Google Analytics Integration 1

less than a minute read

When you integrate with Google Analytics, marketing cloud will add tags to all your links. When this happens, all dynamic links will be broken. Links that are defined programmatically as variable, field or attribute with GA integrated will look as if the tracking link also appends google analytics tags. This will make such link not … Read more

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

SFMC TIP | Open a case

less than a minute read

As the title says open a case every time you experience issue or some feature you used on another instance and now is gone. Why open a case? For example when you automation fails with detailed error “Error has occurred”. Simple solution is to “open a case”. Support will tell you exactly what happened. You … Read more

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

SFMC TIP | Data views for transactional emails

1 minute read

Data views in Salesforce Marketing Cloud are very handy, when it comes to getting data insights from various sources inside the platform. I will give you a quick start on how to get data insights for you transactional emails using data views. We will use data from _Sent, _Job, and _Subscriber view s.TriggererSendDefinitionObjectID – object … Read more

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

SMFC TIP | NULL value comparison in IF statement

less than a minute read

If you have field in DE that its value is not always populated for a particular row (customer) and you test field value against any value cloud page will throw an 500 – internal server error. To fix this you will need to add another AND-condition to test field value for NULL

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
SFMC tips and tricks
Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks

SFMC TIP – Double quotes in AMPScript break email template

less than a minute read

Very recently I have found that when I add any AMPScript function to eg. button URL field SFMC will just ignore everything that follows after the first occurrence of the double quote. Simple fix is to use single quotes instead.

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

SMFC TIP | How to handle form submission

1 minute read

On many occasions when handling any form submission single cloud page application is used. This is great as all of error handling, form submission and processing is on one page so it is actually simpler to implement. But here are some thing you need to take into consideration. Handle GET form submissions I have seen … Read more

Continue reading