Adobe Campaign, Marketing Automation

Enable newest ES6 JavaScript features

Enable newest ES6 JavaScript features

You may have noticed, when working with the tool for some time, that the JavaScript used around the Adobe Campaign Classic platform is not the newest version.

Adobe Campaign Classic relies on ES5 JavaScript, released in 2012. This may cause some first world problems for those accustomed to modern features like Array.indexOf, Array.find, classes, object spread syntax, and arrow functions. Fortunately, polyfills can bridge the gap, providing compatibility with these features and easing the transition to more contemporary JavaScript functionality.

A polyfill in JavaScript is a piece of code (usually JavaScript) that provides the implementation of a feature or API that is not natively supported in a particular environment, typically an older browser or runtime. Polyfills enable developers to use modern features in environments that lack native support.

Array.find()

// Polyfill for Array.prototype.find
if (!Array.prototype.find) {
  Array.prototype.find = function(predicate, thisArg) {
    if (this == null) {
      throw new TypeError('Array.prototype.find called on null or undefined');
    }

    if (typeof predicate !== 'function') {
      throw new TypeError('predicate must be a function');
    }

    var list = Object(this);
    var length = list.length >>> 0;

    for (var i = 0; i < length; i++) {
      if (i in list && predicate.call(thisArg, list[i], i, list)) {
        return list[i];
      }
    }

    return undefined;
  };
}

// Sample array of objects
var users = [
  { id: 1, name: 'Alice', age: 25 },
  { id: 2, name: 'Bob', age: 30 },
  { id: 3, name: 'Charlie', age: 22 },
  { id: 4, name: 'David', age: 28 }
];

// Using Array.prototype.find with the polyfill to find a user with age greater than 25
var result = users.find(function(user) {
  return user.age > 25;
});

logInfo(JSON.stringify(result));

Array.indexOf()

This method is used to find the index of the first occurrence of a specified element in an array.

// Polyfill for Array.prototype.indexOf
if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(searchElement, fromIndex) {
    if (this == null) {
      throw new TypeError('"this" is null or not defined');
    }

    var array = Object(this);
    var length = array.length >>> 0;

    fromIndex = fromIndex || 0;

    for (var i = Math.max(fromIndex, 0); i < length; i++) {
      if (i in array && array[i] === searchElement) {
        return i;
      }
    }

    return -1;
  };
}

Array.fill()

Method that fills all the elements of an array from a start index to an end index with a static value.

if (!Array.prototype.fill) {
  Array.prototype.fill = function(value, start, end) {
    var array = this;
    start = start || 0;
    end = end === undefined ? array.length : end;
    for (var i = start; i < end; i++) {
      array[i] = value;
    }
    return array;
  };
}

Array.entries() Array.keys() and Array.values()

These methods return iterators for array entries, keys, and values, respectively.

// ES6
const numbers = [1, 2, 3];
const entries = numbers.entries(); // iterator for [0, 1], [1, 2], [2, 3]
const keys = numbers.keys();       // iterator for 0, 1, 2
const values = numbers.values();   // iterator for 1, 2, 3

Polyfills in ES5

if (!Array.prototype.entries) {
  Array.prototype.entries = function() {
    var index = 0;
    var array = this;
    return {
      next: function() {
        return index < array.length ?
          { value: [index, array[index++]], done: false } :
          { value: undefined, done: true };
      }
    };
  };
}

if (!Array.prototype.keys) {
  Array.prototype.keys = function() {
    var index = 0;
    var array = this;
    return {
      next: function() {
        return index < array.length ?
          { value: index++, done: false } :
          { value: undefined, done: true };
      }
    };
  };
}

if (!Array.prototype.values) {
  Array.prototype.values = function() {
    var index = 0;
    var array = this;
    return {
      next: function() {
        return index < array.length ?
          { value: array[index++], done: false } :
          { value: undefined, done: true };
      }
    };
  };
}

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.

#adobe campaign classic #JavaScript #programming
Marcel Szimonisz
Marcel Szimonisz
MarTech consultant As a marketing automation consultant, I specialize in problem-solving, process automation, and driving innovation for clients' marketing platforms.

I hold certifications in Adobe Campaign v6 (3x certified) and Salesforce Marketing Cloud (5x certified), as well as 1x Salesforce Associate certified.

Moreover, I serve as a community advisor for Adobe Campaign, providing expert insights and guidance.

Beyond my professional pursuits, I explore various programming languages, CMSs, and frameworks, 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 REST over SOAP
Adobe Campaign, Marketing Automation

Build REST over SOAP API in adobe campaign

4 minutes read

If you’re familiar with Adobe Campaign Classic, you may have noticed that it utilizes the SOAP (Simple Object Access Protocol) API. We live in an age where REST is taking over, and the good old XML SOAP is slowly being forgotten. To me, as an old-timer, I can confidently say that it doesn’t make any […]

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

Redirect 500 internal server error inside try catch SSJS block

1 minute read

If you have Redirect function inside the try catch block, either as mixture of SSJS with AMPScript or only in SSJS, it will throw an 500 – Internal Server Error. Let’s take a look on few examples what can create an error that will take you some quality debugging time to figure it out. And […]

Continue reading
An interactive whiteboard in an educational setting displays a query expression builder, highlighting the use of variables in a complex query. A group of engaged learners discuss the color-marked variables. The room is equipped with data analysis and programming resources, enhancing the learning atmosphere.
ACC Tips & Tricks, Adobe Campaign, Marketing Automation

Use variables in query expression builder

2 minutes read

When the time comes, you will find yourself in a situation where you would like to set up query builder condition expressions as dynamic values. We can achieve this and more with Adobe Campaign Classic. But what are those situations? For example, I had to build a reporting workflow for automated campaigns that was divided […]

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

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 […]

Continue reading
Queries in SSJS and AMPScript
Marketing Automation, Salesforce Marketing Cloud

Query data extensions with SSJS and AMPScript

1 minute read

There’s another topic for which official documentation often lacks sufficient information, but it can be incredibly useful when needed. It’s important to note that in AMPScript, we use the data extension name to reference the table for any query. On the other hand, in SSJS, we utilize the data extension’s external key to reference the […]

Continue reading