Let's dive in to SSJS
In the dynamic landscape of Salesforce Marketing Cloud (SFMC), effective data management is paramount. One critical aspect is data retention, which ensures that records and data extensions are appropriately maintained over time. In this article, we delve into the concept of data retention, explore its significance, and provide a streamlined solution for managing it efficiently.
Understanding Data Retention in SFMC
Data retention policies serve as a mechanism to automatically remove outdated or unnecessary data from your Marketing Cloud environment. When creating a new data extension within Email Studio, administrators are presented with the option to define a data retention policy. This decision impacts how long records within that data extension will persist before being purged.
The Challenge
Imagine a scenario where your organization has already established hundreds of data extensions (DEs). Each DE contains valuable information, but managing data retention policies for all of them manually is a daunting task. Navigating through each DE individually to set retention periods can be time-consuming and error-prone.
The Solution
Thankfully, there’s a more efficient approach. By leveraging Server-Side JavaScript (SSJS), you can automate the process of setting data retention policies across multiple DEs.
Here’s how:
1. Identify the DEs: Begin by compiling a list of all relevant data extensions. Understand their purposes, associated data, and retention requirements.
2. Write SSJS Code: Craft SSJS code snippets that define the desired retention periods for each DE. These scripts will instruct SFMC on when to delete records from specific data extensions.
3. Execute the Code: Run the SSJS code within your Marketing Cloud instance. You can do this via Automation Studio, Script Activities, or custom-built applications. The code will iterate through the DEs, applying the specified retention policies consistently.
4. Monitor and Maintain: Regularly review and adjust your data retention policies as needed. SSJS allows flexibility, so you can adapt to changing business requirements seamlessly.
Retention options
Here is the list of the available and their description from the official documentation.
Example SSJS Code
Having explored Data Retention options and their significance, we now focus on integrating these choices into our existing script. However, a challenge arises: presenting all options simultaneously could lead to errors. To address this, we’ll approach it use-case by use-case, ensuring that conflicting options are avoided.
Lets look in to some example:
Delete individual records after a year (or 365 days)
var attrs = {
DataRetentionPeriodLength: 365,
DataRetentionPeriod: "Days",
RowBasedRetention: true
};
Delete all records and data extensions after 4 weeks (reset on import)
var attrs = {
DataRetentionPeriodLength: 4,
DataRetentionPeriod: "Weeks",
RowBasedRetention: false,
ResetRetentionPeriodOnImport: true
};
Full Code:
Here, our objective is to establish a data retention policy that involves automatically deleting individual records after a year (equivalent to 365 days).
<script runat="server">
Platform.Load("Core", "1");
try{
var externalKey = "6DE949C5-0BA3-4CEXXXXXXXXXX";
/--- give te DE external key here ---
var de = DataExtension.Init(externalKey);
var attrs = {
DataRetentionPeriodLength: 365,
DataRetentionPeriod: "Days",
RowBasedRetention: true
};
var result = de.Update(attrs);
Write("result:" + Stringify(result)); // can be remove
}
catch(ex){
Write("Error:" +Stringify(ex)); // can be remove
}
</script>
Courtesy: https://ampscript.xyz/how-tos/how-to-create-a-de-with-data-retention-options-using-ssjs/
Result
Before setting data retention:
After setting data retention:
Conclusion
By harnessing SSJS, you can efficiently manage data retention policies across your Marketing Cloud ecosystem. Say goodbye to manual adjustments and hello to streamlined data governance. Remember, automation empowers you to focus on strategic initiatives while ensuring data hygiene and compliance.
So, the next time you encounter the challenge of setting data retention for numerous DEs, embrace SSJS—it’s your key to maintaining a well-organized and efficient Marketing Cloud environment.