Efficient Data Extension Folder Structure Management with SSJS: Organize and Optimize Your Data Assets
Organize and Optimize Your Data Assets
The title "Efficient Data Extension Folder Structure Management with SSJS: Organize and Optimize Your Data Assets" highlights the importance of effectively managing folder structures for Data Extensions using SSJS (Server-Side JavaScript). This approach enables you to efficiently organize and optimize your data assets, resulting in improved data management, easier access, and streamlined processes.
Explanation of Folder.Add function via SSJS:
The Folder.Add function in SSJS allows you to create new folders within your Marketing Cloud account's Data Extensions section. With this function, you can specify the parent folder in which the new folder should be created and provide a name for the folder. This helps maintain a well-structured hierarchy, making it easier to categorize and locate Data Extensions based on specific criteria or purposes. By leveraging the Folder.Add function, you can programmatically create folders to suit your organization's data management needs, automating the process and ensuring consistency.
Explanation of DataExtension.Add function via SSJS:
The DataExtension.Add function in SSJS enables you to programmatically create new Data Extensions within a specified folder. By utilizing this function, you can define the properties of the Data Extension, such as its name, fields, and data types. This allows for dynamic creation of Data Extensions, making it convenient to handle large datasets or automate the creation of Data Extensions for specific purposes. The DataExtension.Add function plays a crucial role in efficiently managing and organizing your data assets within the Marketing Cloud platform, simplifying data storage and retrieval processes.
Explanation of Folder.Add function with allowChildren property via SSJS:
In addition to creating new folders within the Data Extensions section, the Folder.Add function in SSJS also provides the option to set the allowChildren property. The allowChildren property determines whether subfolders can be created within the newly created folder. By setting allowChildren to true, you enable the ability to create subfolders within the folder. This can be useful if you have a hierarchical structure for organizing your Data Extensions and want to create a folder that acts as a parent folder for other folders. On the other hand, setting allowChildren to false restricts the creation of subfolders within the folder. This can be beneficial if you want to ensure a flat structure where all Data Extensions are directly stored within the parent folder without any further nesting. By leveraging the allowChildren property in conjunction with the Folder.Add function, you have control over the folder's capabilities and can define whether it can have subfolders or not. This flexibility allows you to tailor your folder structure to best suit your data organization requirements and optimize your data management processes.
Defining a unique name and customer key while creating a new object like a folder or Data Extension is crucial for several important reasons:
- Uniqueness and Identification: A unique name and customer key help in accurately identifying and distinguishing the created object from others. It ensures that each object has a distinct identifier within the system, which is essential for organizing, referencing, and retrieving data later.
- Data Integrity: A unique name and customer key prevent any potential conflicts or overlaps with existing objects. If multiple objects have the same name or customer key, it can lead to data integrity issues, such as data being overwritten or mistakenly associated with the wrong object. By enforcing uniqueness, you maintain the integrity of your data and avoid data inconsistencies.
- Referencing and Querying: When working with objects in Marketing Cloud, such as folders or Data Extensions, you often need to reference or query them using their names or customer keys. Having unique identifiers ensures accurate and reliable referencing, making it easier to perform operations, access data, or retrieve specific objects within your system.
- Automation and Integration: Unique identifiers are particularly crucial when automating processes or integrating with external systems. They provide a reliable and consistent way to identify and interact with specific objects programmatically. Whether you're using APIs, scripting languages like SSJS, or integrating with other platforms, unique names and customer keys ensure seamless automation and integration workflows.
- Governance and Compliance: Unique identifiers play a vital role in governance and compliance practices. They allow for proper tracking, auditing, and management of objects within your system. By having unique names and customer keys, you can maintain a clear record of object creation, modification, and usage, enhancing accountability and adherence to data governance policies.
Define Folder structure via SSJS
Our Folder structure would look like :
Full SSJS Code to build the folder structure with required data extensions :
<script runat="server">
Platform.Load("core","1");
//=================================================================================//
// Reteireive Category Id from the folder based on Name and ContentType
// We are using complex filter because we might be
// using the same name across different assets
//=================================================================================//
function RetrieveCategortyIDForAFolder(name,contentType){
// Assign variables to a filter operations for left filter
var propName1="Name";
var simpleOperator1="equals";
var propValue1=name;
// Assign variables to a filter operations for right filter
var propName2="ContentType";
var simpleOperator2="equals";
var propValue2=contentType;
var filter1={
Property:propName1,SimpleOperator:simpleOperator1,Value:propValue1};
var filter2={
Property:propName2,SimpleOperator:simpleOperator2,Value:propValue2};
var complexFilter = {
LeftOperand: filter1,LogicalOperator: "AND",RightOperand: filter2};
var results = Folder.Retrieve(complexFilter);
return results[0].ID;
}
//=================================================================================//
// Create a sub-folder inside a parent folder based on category ID
// Assign a javascript variable with all prop required for creating the folder
//=================================================================================//
function CreateAfolder(name,customerkey,description,contentType,allowChildren,parentFolderCategoryID){
var newFolder = {
"Name" : name,
"CustomerKey" : customerkey,
"Description" : description,
"ContentType" : contentType,
"IsActive" : "true",
"IsEditable" : "true",
"AllowChildren" : allowChildren,
"ParentFolderID" : parentFolderCategoryID
};
var status = Folder.Add(newFolder);
return status;
}
//======================================================================================//
// Retrieve the category ID from the data extension folder
// folder prop and folder value (Name: value or CustomerKey : value) needs to be supplied
//=======================================================================================//
function retrieveCategorgyIDForDataExtensionCreation(prop,val)
{
var Folderprop =prop;
var Folderval = val;
var FindDE =
Folder.Retrieve({
Property:"Name",SimpleOperator:"equals",Value:Folderval}
);
// if you want to retrieve the parent folder assign "FindDE[0].ParentFolder.ID"
var FolderID = FindDE[0].ID //FindDE[0].ParentFolder.ID;
var FolderName = FindDE[0].Name;
return FolderID;
}
//==================================================================================//
// Create a data extension by passing data extension name,
// folder prop and folder value (Name: value or CustomerKey : value)
//==================================================================================//
function createDataExtension(config) {
// set ws-proxy object
var api = new Script.Util.WSProxy();
//set MID
api.setClientId({
"ID": Platform.Function.AuthenticatedMemberID()
});
var result = api.createItem("DataExtension", config);
api.resetClientIds();
return result;
}
//======================================================================================//
// Define data extension attributes for creating Profile Data Extension
//
//=======================================================================================//
function defineProfileDataExtension(customerKey,name,propName,propVal)
{
var config = {
"CustomerKey":customerKey,
"Name": name,
"CategoryID": retrieveCategorgyIDForDataExtensionCreation(propName,propVal),
"Fields": [
{
"Name": "FirstName",
"FieldType": "Text",
"MaxLength": 50
}
,
{
"Name": "LastName",
"FieldType": "Text",
"MaxLength": 80
}
,
{
"Name": "SubscriberKey",
"FieldType": "Text",
"MaxLength": 254,
"IsPrimaryKey": true,
"IsRequired" : true
}
,
{
"Name": "EmailAddress",
"FieldType": "EmailAddress",
"IsRequired" : true
}
],
"DataRetentionPeriodLength": 4,
"RowBasedRetention": false,
"ResetRetentionPeriodOnImport": true,
"DeleteAtEndOfRetentionPeriod": false,
"DataRetentionPeriod": "Weeks"
};
var result=createDataExtension(config);
return result;
}
//======================================================================================//
// Define data extension attributes for creating tracking Data Extension
//
//=======================================================================================//
function defineTrackingDataExtension(customerKey,name,propName,propVal)
{
var config = {
"CustomerKey": customerKey,
"Name": name,
"CategoryID": retrieveCategorgyIDForDataExtensionCreation(propName,propVal),
"Fields": [
{
"Name": "EventType",
"FieldType": "Text",
"MaxLength": 50
}
,
{
"Name": "EventDate",
"FieldType": "Date"
}
,
{
"Name": "SubscriberKey",
"FieldType": "Text",
"MaxLength": 254,
"IsPrimaryKey": true,
"IsRequired" : true
}
,
{
"Name": "EmailAddress",
"FieldType": "EmailAddress",
"IsRequired" : true
}
],
"DataRetentionPeriodLength": 4,
"RowBasedRetention": false,
"ResetRetentionPeriodOnImport": true,
"DeleteAtEndOfRetentionPeriod": false,
"DataRetentionPeriod": "Weeks"
};
var result=createDataExtension(config);
return result;
}
try{
// Parent Folder definiton
var parentFolderCategoryID= RetrieveCategortyIDForAFolder("Data Extensions","dataextension");
// Campaigns folder deinition
var status=CreateAfolder("Camapigns","Camapigns","Camapigns folder","dataextension","true",parentFolderCategoryID);
Write(status + '\n');
var campaignsCategoryID=RetrieveCategortyIDForAFolder("Camapigns","dataextension");
// B2B Definition
var statusB2B=CreateAfolder("B2B","B2B","B2B Camapigns folder","dataextension","true",campaignsCategoryID);
var b2bProfile=defineProfileDataExtension("B2B Profile","B2B Profile","Name","B2B");
var b2bTracking=defineTrackingDataExtension("B2B Tracking","B2B Tracking","Name","B2B");
Write(statusB2B + '\n');
// B2C Definition
var statusB2C=CreateAfolder("B2C","B2C","B2C Camapigns folder","dataextension","true",campaignsCategoryID);
var b2cProfile=defineProfileDataExtension("B2C Profile","B2C Profile","Name","B2C");
var b2cTracking=defineTrackingDataExtension("B2C Tracking","B2C Tracking","Name","B2C");
Write(statusB2C + '\n');
// Transactional Definition
var statusTransactional=CreateAfolder("Transactional","Transactional","Transactional Camapigns folder","dataextension","true",campaignsCategoryID);
var transactionalProfile=defineProfileDataExtension("Transactional Profile","Transactional Profile","Name","Transactional");
var transactionalTracking=defineTrackingDataExtension("Transactional Tracking","Transactional Tracking","Name","Transactional");
Write(statusTransactional + '\n');
}
catch(ex)
{
Write(Stringify(ex));
}
</script>
Once we publish the code, appropriate folder structure and data extensions would be created :
Comments
Post a Comment