SSJS Introduction - Session 1

 SSJS Introduction - Session 1












Example 1:


<script runat='server' language='javascript'>
Platform.Load('core','1')
try
{
    var MemberID = Platform.Recipient.GetAttributeValue('memberid');
    var subjectLine="SSJS Enablement 1"
    Variable.SetValue("@subjectLine",subjectLine)
    Write(MemberID);
 
}
catch(ex)
{
    Write(ex.message);
}
</script>

%%[if 1==0 then]%%
<a href="%%profile_center_url%%" alias="Update Profile">Update Profile</a>
<table cellpadding="2" cellspacing="0" width="600" ID="Table5" Border=0>
<tr><td><font face="verdana" size="1"
color="#444444">
    This email was sent by:
<b>%%Member_Busname%%</b><br>%%Member_Addr%% %%Member_City%%,
     %%Member_State%%, %%Member_PostalCode%%, %%Member_Country%%
<br><br></font></td></tr></table>
%%[endif]%%

Example 2:


<script runat="server">
   Platform.Load("core", "1.1.5");
    try{
       
    //use either "Name" or "Customer Key"
    var DEprop ="Name"
    //provide either the Name or External Key
    var DEval = "HOLIDAYS"
    //Retrieve the DE
    var FindDE = DataExtension.Retrieve({Property:DEprop,SimpleOperator:"equals"
,Value:DEval});
    //Assign the folder ID
    var FolderID = FindDE[0].CategoryID;
    //Assign the DE name
    var DEname = FindDE[0].Name;
    var list = [];
    list.push(DEname);
    var path = function(id) {
    if (id> 0) {
        var results = Folder.Retrieve({Property:"ID",SimpleOperator:"equals",
Value:id});
    list.unshift(results [0].Name);
    return path(results[0].ParentFolder.ID);
    } else {
        return id;
    }
};
    path(FolderID);
Write(list.join("> "));
}catch(ex){
        Write(ex.message);
}
</script>


Example 3:

<script runat="server">
  //Load the Core server-side JavaScript library
  Platform.Load("core", "1");
  //retrieve the folder ID by passing property and value
  // as key-value pair for the search
  function retrieveCategorgyID(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(dataExtensionName,folderProp,folderVal) {
    // set ws-proxy object
    var api = new Script.Util.WSProxy();
    //set MID
    api.setClientId({
      "ID": Platform.Function.AuthenticatedMemberID()
    }
                   );
    //Configures specific object from identified SOAP Object
    var config = {
      "CustomerKey": String(Platform.Function.GUID()).toUpperCase(),
      "Name": dataExtensionName,
      "CategoryID": retrieveCategorgyID(folderProp,folderVal),
      "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 = api.createItem("DataExtension", config);
    return(Stringify(result));
  };
  try{
    var result=createDataExtension("My Data Extension","Name","Episode 13")
    Write(result);
  }
  catch(e){
    Write(e.message)}
</script>


Example 4:

<scrtipt runat="server" language="Javascript">
    Platform.Load("Core","1");


    try{
    var deNamekey="SSJSSession1";
    var sourceDE=DataExtension.Init(deNamekey);
    var sourceFields=sourceDE.Fields.Retrieve();

    var fieldsArray=[];

    for(var i=0; i < sourceFields.length; i++){
    var fields={ };
    fields.Name=sourceFields[i].Name;
    fileds.FieldType=sourceFields[i].FieldType;

    if(sourceFields[i].FieldType=="Text"){
        fileds.Maxlength = sourceFields[i].Maxlength;
}

    if(sourceFields[i].IsPrimaryKey){
        fileds.IsPrimaryKey = "true";
    fileds.IsRequired="true";
}

    fieldsArray.push(fields);

}

    Write(Stringify(fieldsArray));
}
catch(ex){
    Write(ex.message);
}


</scrtipt>


Example 5:

<script runat="server">
     //Load the Core server-side JavaScript library
    Platform.Load("core", "1");

    var Folderprop ="Name";
    var Folderval = "Lakshmi";
    var FindDE =
    Folder.Retrieve(
    {Property:Folderprop,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;
    Write( FolderID);
</script>









Reference :



Comments


Knowledge Article

Most Viewed

CLOUD PAGE ENABLEMENT - PART 1

EMAIL NOT SENT IN JOURNEY BUILDER

CONSIDERATIONS FOR JOURNEY BUILDER

Journey Builder REST API Documentation

Preference Center Demystified

Popular Posts

CLOUD PAGE ENABLEMENT - PART 1

EMAIL NOT SENT IN JOURNEY BUILDER

CONSIDERATIONS FOR JOURNEY BUILDER

Journey Builder REST API Documentation

Preference Center Demystified

SEND LOG EANBLEMENT

Share with Friends

Disclaimer:

The information provided on this technical blog is for general informational purposes only. As a SFMC (Salesforce Marketing Cloud) Technical Architect, I strive to offer accurate and up-to-date content related to SFMC and its associated technologies. However, please note that technology is constantly evolving, and the information provided may become outdated or inaccurate over time.

The content published on this blog represents my personal views and experiences as a SFMC Technical Architect and does not necessarily reflect the official views or opinions of any organization or employer I may be affiliated with.

While I make every effort to ensure the accuracy and reliability of the information presented, I cannot guarantee its completeness, suitability, or applicability to your specific circumstances. Therefore, it is essential to verify any information provided and make your own independent assessments or seek professional advice if needed.

Furthermore, any actions taken based on the information provided on this blog are at your own risk. I shall not be held liable for any damages, losses, or inconveniences arising from the use of the information presented here.

Please keep in mind that SFMC and its associated technologies are complex and require technical expertise for proper implementation and management. It is recommended to consult with qualified professionals or official SFMC documentation for comprehensive guidance.

Finally, please note that any product or company names mentioned on this blog are trademarks or registered trademarks of their respective owners. The mention of these trademarks or registered trademarks does not imply any endorsement or affiliation with the blog.

By accessing and using this blog, you agree to the terms of this disclaimer. If you do not agree with any part of this disclaimer, please refrain from using this blog.