SSJS: Day 20 focused on Resource

SSJS: Day 20 focused on Resource

Write an SSJS for Building Campaign-Specific Folder Structure in Marketing Cloud.

No alt text provided for this image
https://www.youtube.com/@b2shashi


<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>        

To view or add a comment, sign in

Others also viewed

Explore topics