Create via WSProxy

To create a single item or several items of the same type in a single call, use the createItem() and createBatch() methods.

  • For the first parameter, specify the object type to perform the action on, such as Email or DataExtension.
  • The second parameter is an object that represents the fields and values to set on the object. For batch operations, the second parameter is an Array of objects rather than a single object.
  • The third parameter is optional and includes any properties to set using the CreateOptions object.

Create a Single DataExtension Object

This example creates a simple DataExtension object that contains an ID and a Name field.

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

var guid = Platform.Function.GUID();
var name = "my test de - " + guid;

var de = {
    Name: name,
    CustomerKey: guid,
    Description: "Another DE added via SSJS",
    Fields: [{
        FieldType: "Text",
        Name: "ID",
        MaxLength: 36,
        IsPrimaryKey: true,
        IsNillable: false,
        IsRequired: true
    },
    {
        FieldType: "Text",
        Name: "Name",
        MaxLength: 200
    }],
    CategoryID: 101377
}

var res = prox.createItem("DataExtension", de);

Create Multiple DataExtension Objects

This example creates two similar data extensions using the createBatch function.

var prox = new Script.Util.WSProxy();
var guid = Platform.Function.GUID();
var name = "my test de - 1 - " + guid;

var de1 = {
    Name: name,
    CustomerKey: guid,
    Description: "Another DE added via SSJS",
    Fields: [{
        FieldType: "Text",
        Name: "ID",
        MaxLength: 36,
        IsPrimaryKey: true,
        IsNillable: false,
        IsRequired: true
    },
    {
        FieldType: "Text",
        Name: "Name",
        MaxLength: 200
    }],
    CategoryID: 101377
}
guid = Platform.Function.GUID();
name = "my test de - 2 - " + guid;

var de2 = {
    Name: name,
    CustomerKey: guid,
    Description: "Another DE added via SSJS",
    Fields: [{
        FieldType: "Text",
        Name: "ID",
        MaxLength: 36,
        IsPrimaryKey: true,
        IsNillable: false,
        IsRequired: true
    },
    {
        FieldType: "Text",
        Name: "Name",
        MaxLength: 200
    }],
    CategoryID: 101377
}
var res = prox.createBatch("DataExtension", [ de1, de2 ]);

In both single and batch operations, the results contain three properties: Status, RequestID, and Results. The createItem() method returns one item, and the createBatch() method returns the same number of items that were passed into the method.

{
    "Status": "OK",
    "RequestID": "fb768ddc-6670-4183-8b9d-4f0d5518bb2e",
    "Results": [...]
}

Example: Create an Automation Activity

This example creates an automation activity and includes the Raw SOAP Wrapper which corresponds with the WSProxy call.

<script runat="server">
  try {
    /* Initialize the WSProxy Object */
    var api = new Script.Util.WSProxy();

    /* Set ClientID */
    api.setClientId({
      "ID": Platform.Function.AuthenticatedMemberID(),
      "UserID": Platform.Function.AuthenticatedEmployeeID()
    });

    /* Build Query Object */
    var automation = {
      "Name": "Example Automation",
      "CustomerKey": "ExampleAutomation",
      "Description": "My Automation Example",
      "AutomationTasks": [
        {
          "Name": "Task 1",
          "Activities": [
            {
              "ObjectID": "a582746c-4e38-4552-b810-d6eaeb76ed2d",
              "Name": "Query 1",
              "ActivityObject": {
                "__Type__": "QueryDefinition",
                "ObjectID": "a582746c-4e38-4552-b810-d6eaeb76ed2d",
                "Name": "Example Query Activity",
                "CustomerKey": "ExampleQueryActivity"
              }
            }
          ]
        }
      ],
      "AutomationType": "Scheduled"
    };

    /* Create via API */
    var result = api.createItem('Automation', automation);
    Platform.Response.Write(Platform.Function.Stringify(result));
  } catch(e) {
    Platform.Response.Write(Platform.Function.Stringify(e));
  };
</script>

The response includes information about the Automation Activity.

{
  "Status": "OK",
  "RequestID": "eb0dccf7-ce17-4f9b-9427-fffc8c4cf034",
  "Results": [
    {
      "NewID": 0,
      "NewObjectID": "9c825220-6f2f-40f2-95c2-e44909afed87",
      "PartnerKey": null,
      "Object": {
        "Schedule": null,
        "AutomationTasks": [
          {
            "AutomationTaskType": null,
            "Name": null,
            "Description": null,
            "Automation": null,
            "Sequence": 0,
            "Activities": [null],
            "Client": null,
            "PartnerKey": null,
            "PartnerProperties": null,
            "CreatedDate": "0001-01-01T00:00:00.000",
            "ModifiedDate": null,
            "ID": 0,
            "ObjectID": null,
            "CustomerKey": null,
            "Owner": null,
            "CorrelationID": null,
            "ObjectState": null,
            "IsPlatformObject": false
          }
        ],
        "IsActive": true,
        "AutomationSource": null,
        "Status": 0,
        "Notifications": null,
        "ScheduledTime": "0001-01-01T00:00:00.000",
        "AutomationType": "Scheduled",
        "UpdateModified": false,
        "LastRunInstanceID": null,
        "CreatedBy": 0,
        "CategoryID": null,
        "LastRunTime": "0001-01-01T00:00:00.000",
        "LastSaveDate": "0001-01-01T00:00:00.000",
        "ModifiedBy": 0,
        "RecurrenceID": null,
        "LastSavedBy": 0,
        "InteractionObjectID": null,
        "Name": "Example Automation",
        "Description": "My Automation Example",
        "Keyword": null,
        "Client": {
          "ID": 987654,
          "ClientID1": 0,
          "PartnerClientKey": null,
          "UserID": 998877665,
          "PartnerUserKey": null,
          "CreatedBy": 0,
          "ModifiedBy": 0,
          "EnterpriseID": 0,
          "CustomerKey": null,
          "CustomerID": null
        },
        "PartnerKey": null,
        "PartnerProperties": [{ "Name": "AutomationType", "Value": "Scheduled" }],
        "CreatedDate": "0001-01-01T00:00:00.000",
        "ModifiedDate": null,
        "ID": 0,
        "ObjectID": "9c825220-6f2f-40f2-95c2-e44909afed87",
        "CustomerKey": "ExampleAutomation",
        "Owner": null,
        "CorrelationID": null,
        "ObjectState": null,
        "IsPlatformObject": false
      },
      "CreateResults": null,
      "ParentPropertyName": null,
      "StatusCode": "OK",
      "StatusMessage": null,
      "OrdinalID": 0,
      "ErrorCode": 0,
      "RequestID": null,
      "ConversationID": null,
      "OverallStatusCode": null,
      "RequestType": "Synchronous",
      "ResultType": null,
      "ResultDetailXML": null
    }
  ]
}