Adobe Campaign, Marketing Automation

Do Loops in Adobe Campaign worfklow

Do Loops in Adobe Campaign worfklow

At times, there may be a need to execute the same workflow or its parts in loops. I’ll guide you through the process of creating your very own “for loop” within the Adobe Campaign workflow. loop

While loop

A “while loop” programming construct used to repeatedly execute a block of code as long as a specified condition remains true. Unlike a “for loop,” a “while loop” doesn’t have a predetermined number of iterations. Instead, it continues to execute as long as the specified condition is true.

The basic structure of a while loop typically includes:

  • Condition: Specifies the condition that must be true for the loop to continue. If the condition evaluates to false, the loop terminates, and the program moves on to the next set of instructions.
  • Code to be repeated: The set of statements or code block that gets executed in each iteration. This block continues to execute as long as the condition remains true.

For loop

A “for loop” is a programming construct used to repeat a set of statements or a block of code a specified number of times. It is particularly useful when you need to perform a task repeatedly, such as iterating through elements in a collection, processing data, or executing a series of operations with incremental or varying values.

The basic structure of a for loop typically includes:

  • Initialization: Setting an initial value for a control variable.
  • Condition: Defining a condition that, when true, allows the loop to continue.
  • Update: Modifying the control variable after each iteration.

Adobe Campaign loop types

For loops in Adobe Campaign Classic can be for instance:

  • handling duplicate entries separately in distinct email sends
  • dispatching proofs for various delivery templates
  • sending / receiving data from API where there is payload/ response limitation per call
  • and many more…

From my experience I can divide for loops into two categeries:

  • no codefor loop – you will process data defined in the workflow targeting data
  • code for loop – you will process array that is defined as JSON variable and passed in vars. variable down the workflow

No code loop

We begin with a slightly simpler loop scenario where we navigate through targeting data outlined in the workflow. Recently, the need arose to deduplicate the population before the actual send. This became imperative as we were relaying changes to contracts, and a specific business requirement dictated the identification of which contract was sent in each email. Regrettably, we couldn’t integrate this information directly into the broad log schema in the target mapping. Consequently, we had to establish a linkage between the broad log record and a distinct table post the actual send, leading to the initiation of a loop functionality.

Adobe Campaign Classic - no code loop example
  • Query activity – select any data you need to process in loops.
  • Test activity – we assess whether to proceed with the loop or conclude it. In this scenario, the loop persists until there are records available for processing. The condition is straightforward: we set the variable recCount to greater than 0, a condition you may have already encountered elsewhere on the platform.

Code loop


In this scenario, the data that we’ll iterate through is defined in a JSON array. You can predefine the array before the JavaScript (JS – shift value) activity, where the first item of the array is picked. The example provided essentially represents a “do-while” loop. If you move the Advanced JavaScript (AJS – condition) condition to the beginning, you essentially create a “while-do” loop.

Adobe Campaign Classic - code loop example

JS define array – In the initial step, we define our array. This could result from an API call, which we then save into the ‘vars.’ using JSON.stringify(). It’s worth noting that ‘vars.’ variables are specifically designed to contain strings. Despite having the ability to define types, objects are not even an option within this context.

var data = [
  {"name":"john","lastname":"doe"},
  {"name":"john","lastname":"doe2"},
  {"name":"john","lastname":"doe3"},
  {"name":"john","lastname":"doe4"}
]

//save data to vars.

vars.data = JSON.stringify(data);

JS – shift value activity is placed at the begining of the “do-while” loop, serving as the point where we extract the initial item from the array. This is accomplished by utilizing the Array.shift() function to retrieve and reduce the array by removing its first element.

GOOD TO KNOW: When handling objects in JavaScript within the workflow, it’s essential to consistently parse the string representation to an object. After making changes, it’s then necessary to convert it back to a string. This ensures seamless movement of the object between workflow activities. We use JSON functions stringify() and parse().

//transform back to array
var data = JSON.parse(vars.data);

//pop value from the begining of the array
var item = data.shift();

//save item to vars.
vars.item = JSON.stringify(item);

//save the remaining items to vars.
vars.data = JSON.stringify(data);

Next section is where we essentially perform operations on the item extracted from the iterated array. My do something part is simply printing the values of my object to the console.

var item = JSON.parse(vars.item);
logInfo("Name: " + item.name)
logInfo("Last name: " + item.lastname);

/* 
console log
---------------
12/06/2023 5:46:55 PM		Workflow finished
12/06/2023 5:46:54 PM	wait	Suspended. Will resume on: 12/06/2023 5:46:55 PM
12/06/2023 5:46:54 PM	wait	Last name: doe4
12/06/2023 5:46:54 PM	wait	Name: john
12/06/2023 5:46:53 PM	wait	Suspended. Will resume on: 12/06/2023 5:46:54 PM
12/06/2023 5:46:53 PM	wait	Last name: doe3
12/06/2023 5:46:53 PM	wait	Name: john
12/06/2023 5:46:51 PM	wait	Suspended. Will resume on: 12/06/2023 5:46:52 PM
12/06/2023 5:46:51 PM	wait	Last name: doe2
12/06/2023 5:46:51 PM	wait	Name: john
12/06/2023 5:46:50 PM	wait	Suspended. Will resume on: 12/06/2023 5:46:51 PM
12/06/2023 5:46:50 PM	wait	Last name: doe
12/06/2023 5:46:50 PM	wait	Name: john
*/


Upon completion of the ‘do something’ section, our final but most crucial step involves the ‘while’ condition check. When this is not set properly we can cause the beatiful infinite loops. At this step, we verify whether the array still contains values. If no more values remain for processing, we gracefully conclude the loop and finalize the workflow.

Following code has been added into the Next calls section of Advanced JavaScript activity

var data = JSON.parse(vars.data);

if(data.length)
  task.postEvent(task.transitionByName('loop'));
else
  task.postEvent(task.transitionByName('stop'));
return 0

Similarly you can apply above mentioned logic to create for loops.

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

The image is a wide-format representation of a modern coding environment, specifically focused on the latest ES6 JavaScript features. It prominently displays a computer screen with a code editor, where JavaScript code is shown with highlights on ES6 features such as arrow functions, classes, and template literals. The code is adorned with vibrant syntax highlighting, making elements like code brackets and semi-colons stand out, emphasizing the JavaScript language. The background is infused with digital and technological elements like binary code patterns and abstract tech-inspired graphics, symbolizing innovation in coding. The overall color scheme is dynamic and lively, reflecting the advanced and cutting-edge nature of ES6 in the realm of web development.
Adobe Campaign, Marketing Automation

Enable newest ES6 JavaScript features

2 minutes read

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

Continue reading
Salesforce Marketing Cloud Tips
Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks

How to add segments to proof subject

1 minute read

Issues arise when an email’s HTML contains personalization that includes multiple different variants, that is altering the appearance of the personalized email. Quality assurance (QA) of such proofs can become exceptionally challenging, particularly in cases where there are minor changes across variants or a substantial number of variants present. You can devide your variants by […]

Continue reading
How to use journey exit criteria and goals
Marketing Automation, Salesforce Marketing Cloud

How to use journey exit criteria and goals

3 minutes read

I might be really slow learning but I learned about this feature two years into working with salesforce marketing cloud, and I said to myself what a nice feature to have. Let me tell you what it is and give you some real life examples when to use it. Exit criteria and goals share a […]

Continue reading
How to fetch code snippet with AMPScript
Marketing Automation, Salesforce Marketing Cloud, SFMC Tips & Tricks

How to fetch code snippet with AMPScript

1 minute read

If you have ever experienced troubles when referring to your content block within another resource, either an email template or a cloud page, using ContentBlockBy AMPScript functions, I will show you a hopefully better way to include your snippets in your content. Dynamic content creation and personalization within Salesforce Marketing Cloud, can often grapple developers […]

Continue reading