1

I have created a HTML landing page like below, which takes the input from the user for Data Extension name

<html>
  <body>
    <h1>
      Create a new Data Extension 
    </h1>
    <form action="{{URL_OF_SSJS_CODE_RESOURCE}}" method="post">
  <label for="dename">Data Extension Name:</label><br>
  <input type="text" name="dename">
      <input type="submit" value="Create & Copy">
</form> 
  </body>
</html>

Then on form submit the DE name is passed to SSJS code resource (in order to create a new DE entered by user) which looks like below

 <script runat="server">
    Platform.Load("Core","1");
    
    try{
    var deName = Request.GetFormField("dename");
    Write("DE Name is...." + deName);
    }

    catch(err)
    {
      Write("error is.."+err);
    }
      
        /*  fetch folder  */
  var FolderAttributes = Folder.Retrieve({ Property: "Name", SimpleOperator: "equals", Value:"Test_Create"});
  var FolderID = FolderAttributes[0].ID; 
    
    //create DE
   try{
   
   var obj1={ 
    "Name" :deName,
    "CustomerKey" : deName,
     "CategoryID": FolderID,
    "Fields": [
     { "Name" : "name", "FieldType" : "Text", "MaxLength" : 50 }
    ] 
    };
    
  /*  function*/ 
  var myDe=DataExtension.Add(obj1);
   }
    
    catch(err)
    {
    Write ("cannot create DE: "+err);
    }
  </script>

The DE Name is passed to the SSJS page but still I am not able to create a DE and get the caught error. Could anyone point where I am doing wrong and how to fix this code so that the DE gets created. When I try to create the DE in automation studio (by passing static values), I can successfully create the DE.

1 Answer 1

0

Upd. Just tested, Your code should work if You will use single try catch instead of two. Just copy this 2 lines of code to the second try and remove first try catch

var deName = Request.GetFormField("dename");
Write("DE Name is...." + deName);

The code below is same as Yours, but uses WSProxy functions and double check of the parent folder (contentType + name). Also it allows to add IsSendable and IsSendableSpecified options to Data Extensions.

<script runat="server">
    Platform.Load("Core", "1");
    var response;

    var soap = new Script.Util.WSProxy();

    /* DE Fields  */
    var TestDETemplate = [
        { Name: 'Test', FieldType: 'Text', IsPrimaryKey: true, IsRequired: true, MaxLength: 254 }
    ]

    try {
        var deName = Request.GetFormField("dename");
        Write("DE Name is...." + deName);

       /* Retrieve DE FolderID */
        response = soap.retrieve('DataFolder', ['ID'], {
            LeftOperand: {
                Property: 'Name',
                SimpleOperator: 'equals',
                Value: 'Test' // Change this value to Yours parent DE Folder Name
            },
            LogicalOperator: 'AND',
            RightOperand: {
                Property: 'ContentType',
                SimpleOperator: 'equals',
                Value: 'dataextension'
            }
        });
        var testDEFolderID = response.Results[0].ID;

        var testDEConfig = {
            Name: deName, // Name
            CustomerKey: deName, // Customer Key
            CategoryID: testDEFolderID, // FolderID
            Fields: TestDETemplate, // DE Fields
            IsSendable: false, // Sendable true/false
            IsSendableSpecified: false 
           // if Sendable true -> 
           // IsSendable: true,
           // IsSendableSpecified: true,
           // SendableDataExtensionField: { Name: 'SubscriberKey' }, 
           // SendableSubscriberField: { Name: 'Subscriber Key' }
        }
        response = soap.createItem('DataExtension', testDEConfig)
    } catch (err) {
        Write("error is.." + err);
    }

</script>
1
  • 2
    Welcome to Salesforce Stack Exchange (SFSE)! Thank you for your contribution. Here on SFSE the why (explanation) is often as or more important than the how (code) in an answer - especially since it can help future searchers/readers. Please edit your answer to explain - even briefly (but enough to understand) - what in your code is different from the code posted in the question, and why that solves the problem.
    – Moonpie
    Commented Mar 15, 2022 at 14:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .