Create a Data Extension verified

Learn how to create a Data Extension in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include WSProxy and Core methods.

Core

var config = {
    "CustomerKey" : GUID(),
    "Name" : "MyDataExtension",
    "CategoryID": 1234,
    "SendableInfo" : {
        "Field" : { 
            "Name" : "SubscriberKey", 
            "FieldType" : "Text" 
        },
        "RelatesOn" : "Subscriber Key"
     },
    "IsTestable": true,
    "Fields" : [
        {
            "Name": "SubscriberKey",
            "FieldType": "Text",
            "MaxLength": 50,
            "IsPrimaryKey": true,
            "IsRequired" : true
        },
        {
            "Name": "FirstName",
            "FieldType": "Text",
            "MaxLength": 50
        },
        {
            "Name": "LastName",
            "FieldType": "Text",
            "MaxLength": 80
        }, 
        {
            "Name": "EmailAddress",
            "FieldType": "EmailAddress"
        }
    ]
};

var result = DataExtension.Add(config);
<script runat="server">

    Platform.Load("core", "1");

	try {

        var config = {
            "CustomerKey" : GUID(),
            "Name" : "MyDataExtension",
            "CategoryID": 1234,
            "SendableInfo" : {
                "Field" : { 
                    "Name" : "SubscriberKey", 
                    "FieldType" : "Text" 
                },
                "RelatesOn" : "Subscriber Key"
            },
            "IsTestable": true,
            "Fields" : [
                {
                    "Name": "SubscriberKey",
                    "FieldType": "Text",
                    "MaxLength": 50,
                    "IsPrimaryKey": true,
                    "IsRequired" : true
                },
                {
                    "Name": "FirstName",
                    "FieldType": "Text",
                    "MaxLength": 50
                },
                {
                    "Name": "LastName",
                    "FieldType": "Text",
                    "MaxLength": 80
                }, 
                {
                    "Name": "EmailAddress",
                    "FieldType": "EmailAddress"
                }
            ]
        };

        var result = DataExtension.Add(config);

        Write(Stringify(result));
		
	} catch(error) {
        Write(Stringify(error));
    }	

</script>
{
    "Fields": {
        "Add": "function",
        "Remove": "function",
        "Retrieve": "function",
        "Update": "function",
        "UpdateSendableField": "function"
    },
    "Remove": "function",
    "Update": "function",
    "Rows": {
        "Lookup": "function",
        "Add": "function",
        "Remove": "function",
        "Update": "function",
        "Retrieve": "function"
    }
}

WSProxy

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

api.setClientId({"ID": Platform.Function.AuthenticatedMemberID()});

var fields = [
    {
        "Name": "SubscriberKey",
        "FieldType": "Text",
        "MaxLength": 50,
        "IsPrimaryKey": true,
        "IsRequired" : true
    },
    {
        "Name": "FirstName",
        "FieldType": "Text",
        "MaxLength": 50
    },
    {
        "Name": "LastName",
        "FieldType": "Text",
        "MaxLength": 80
    }, 
    {
        "Name": "EmailAddress",
        "FieldType": "EmailAddress"
    }
];

var config = {
    "CustomerKey": GUID(),
    "Name": "MyDataExtension",
    "CategoryID": 1234,
    "Fields": fields,
    "SendableDataExtensionField": { 
        "Name" : "SubscriberKey", 
        "FieldType" : "Text" 
    }, 
    "SendableSubscriberField": {
        "Name": "Subscriber Key"
    },
    "IsSendable": true,
    "IsTestable": true,
    "DataRetentionPeriodLength": 7,
    "DataRetentionPeriod": "Days",
    "RowBasedRetention": 0,
    "ResetRetentionPeriodOnImport": 1,
    "DeleteAtEndOfRetentionPeriod": 0
};

var result = api.createItem("DataExtension", config); 
<script runat="server">

    Platform.Load("core", "1");

    var api = new Script.Util.WSProxy();
	
	try {

        api.setClientId({"ID": Platform.Function.AuthenticatedMemberID()});

        var fields = [
            {
                "Name": "SubscriberKey",
                "FieldType": "Text",
                "MaxLength": 50,
                "IsPrimaryKey": true,
                "IsRequired" : true
            },
            {
                "Name": "FirstName",
                "FieldType": "Text",
                "MaxLength": 50
            },
            {
                "Name": "LastName",
                "FieldType": "Text",
                "MaxLength": 80
            }, 
            {
                "Name": "EmailAddress",
                "FieldType": "EmailAddress"
            }
        ];

        var config = {
            "CustomerKey": GUID(),
            "Name": "MyDataExtension",
            "CategoryID": 1234,
            "Fields": fields,
            "SendableDataExtensionField": { 
                "Name" : "SubscriberKey", 
                "FieldType" : "Text" 
            }, 
            "SendableSubscriberField": {
                "Name": "Subscriber Key"
            },
            "IsSendable": true,
            "IsTestable": true,
            "DataRetentionPeriodLength": 7,
            "DataRetentionPeriod": "Days",
            "RowBasedRetention": 0,
            "ResetRetentionPeriodOnImport": 1,
            "DeleteAtEndOfRetentionPeriod": 0
        };

        var result = api.createItem("DataExtension", config); 

        Write(Stringify(result));
		
	} catch(error) {
        Write(Stringify(error));
    }	

</script>
{
    "Status": "OK",
    "RequestID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
    "Results": [
        {
            "NewID": 0,
            "NewObjectID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
            "PartnerKey": null,
            "Object": {
                "CustomerKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
                "Name": "MyDataExtension",
                "Description": null,
                "IsSendable": false,
                "IsTestable": false,
                "SendableDataExtensionField": null,
                "SendableSubscriberField": null,
                "Template": null,
                "DataRetentionPeriodLength": 7,
                "DataRetentionPeriodUnitOfMeasure": 0,
                "RowBasedRetention": false,
                "ResetRetentionPeriodOnImport": true,
                "DeleteAtEndOfRetentionPeriod": false,
                "RetainUntil": null,
                "Fields": [
                    {
                        "CustomerKey": null,
                        "Name": "SubscriberKey",
                        "ObjectID": null,
                        "FieldType": "Text",
                        "IsPrimaryKey": true,
                        "MaxLength": 50,
                        "Ordinal": 0,
                        "DefaultValue": null,
                        "StorageType": "Unspecified",
                        "DataExtension": null,
                        "DataType": null,
                        "ValueType": "xsdstring",
                        "PropertyType": "string",
                        "IsCreatable": false,
                        "IsUpdatable": false,
                        "IsRetrievable": false,
                        "IsQueryable": false,
                        "IsFilterable": false,
                        "IsPartnerProperty": false,
                        "IsAccountProperty": false,
                        "PartnerMap": null,
                        "AttributeMaps": null,
                        "Markups": null,
                        "Precision": 0,
                        "Scale": 0,
                        "Label": null,
                        "Description": null,
                        "MinLength": 0,
                        "MinValue": null,
                        "MaxValue": null,
                        "IsRequired": true,
                        "IsViewable": false,
                        "IsEditable": false,
                        "IsNillable": false,
                        "IsRestrictedPicklist": false,
                        "PicklistItems": null,
                        "IsSendTime": false,
                        "DisplayOrder": 0,
                        "References": null,
                        "RelationshipName": null,
                        "Status": null,
                        "IsContextSpecific": false,
                        "Client": null,
                        "PartnerKey": null,
                        "PartnerProperties": null,
                        "CreatedDate": "0001-01-01T00:00:00.000",
                        "ModifiedDate": null,
                        "ID": 0,
                        "Owner": null,
                        "CorrelationID": null,
                        "ObjectState": null,
                        "IsPlatformObject": false
                    },
                    {
                        "CustomerKey": null,
                        "Name": "FirstName",
                        "ObjectID": null,
                        "FieldType": "Text",
                        "IsPrimaryKey": false,
                        "MaxLength": 50,
                        "Ordinal": 0,
                        "DefaultValue": null,
                        "StorageType": "Unspecified",
                        "DataExtension": null,
                        "DataType": null,
                        "ValueType": "xsdstring",
                        "PropertyType": "string",
                        "IsCreatable": false,
                        "IsUpdatable": false,
                        "IsRetrievable": false,
                        "IsQueryable": false,
                        "IsFilterable": false,
                        "IsPartnerProperty": false,
                        "IsAccountProperty": false,
                        "PartnerMap": null,
                        "AttributeMaps": null,
                        "Markups": null,
                        "Precision": 0,
                        "Scale": 0,
                        "Label": null,
                        "Description": null,
                        "MinLength": 0,
                        "MinValue": null,
                        "MaxValue": null,
                        "IsRequired": false,
                        "IsViewable": false,
                        "IsEditable": false,
                        "IsNillable": false,
                        "IsRestrictedPicklist": false,
                        "PicklistItems": null,
                        "IsSendTime": false,
                        "DisplayOrder": 0,
                        "References": null,
                        "RelationshipName": null,
                        "Status": null,
                        "IsContextSpecific": false,
                        "Client": null,
                        "PartnerKey": null,
                        "PartnerProperties": null,
                        "CreatedDate": "0001-01-01T00:00:00.000",
                        "ModifiedDate": null,
                        "ID": 0,
                        "Owner": null,
                        "CorrelationID": null,
                        "ObjectState": null,
                        "IsPlatformObject": false
                    },
                    {
                        "CustomerKey": null,
                        "Name": "LastName",
                        "ObjectID": null,
                        "FieldType": "Text",
                        "IsPrimaryKey": false,
                        "MaxLength": 80,
                        "Ordinal": 0,
                        "DefaultValue": null,
                        "StorageType": "Unspecified",
                        "DataExtension": null,
                        "DataType": null,
                        "ValueType": "xsdstring",
                        "PropertyType": "string",
                        "IsCreatable": false,
                        "IsUpdatable": false,
                        "IsRetrievable": false,
                        "IsQueryable": false,
                        "IsFilterable": false,
                        "IsPartnerProperty": false,
                        "IsAccountProperty": false,
                        "PartnerMap": null,
                        "AttributeMaps": null,
                        "Markups": null,
                        "Precision": 0,
                        "Scale": 0,
                        "Label": null,
                        "Description": null,
                        "MinLength": 0,
                        "MinValue": null,
                        "MaxValue": null,
                        "IsRequired": false,
                        "IsViewable": false,
                        "IsEditable": false,
                        "IsNillable": false,
                        "IsRestrictedPicklist": false,
                        "PicklistItems": null,
                        "IsSendTime": false,
                        "DisplayOrder": 0,
                        "References": null,
                        "RelationshipName": null,
                        "Status": null,
                        "IsContextSpecific": false,
                        "Client": null,
                        "PartnerKey": null,
                        "PartnerProperties": null,
                        "CreatedDate": "0001-01-01T00:00:00.000",
                        "ModifiedDate": null,
                        "ID": 0,
                        "Owner": null,
                        "CorrelationID": null,
                        "ObjectState": null,
                        "IsPlatformObject": false
                    },
                    {
                        "CustomerKey": null,
                        "Name": "EmailAddress",
                        "ObjectID": null,
                        "FieldType": "EmailAddress",
                        "IsPrimaryKey": false,
                        "MaxLength": 0,
                        "Ordinal": 0,
                        "DefaultValue": null,
                        "StorageType": "Unspecified",
                        "DataExtension": null,
                        "DataType": null,
                        "ValueType": "xsdstring",
                        "PropertyType": "string",
                        "IsCreatable": false,
                        "IsUpdatable": false,
                        "IsRetrievable": false,
                        "IsQueryable": false,
                        "IsFilterable": false,
                        "IsPartnerProperty": false,
                        "IsAccountProperty": false,
                        "PartnerMap": null,
                        "AttributeMaps": null,
                        "Markups": null,
                        "Precision": 0,
                        "Scale": 0,
                        "Label": null,
                        "Description": null,
                        "MinLength": 0,
                        "MinValue": null,
                        "MaxValue": null,
                        "IsRequired": false,
                        "IsViewable": false,
                        "IsEditable": false,
                        "IsNillable": false,
                        "IsRestrictedPicklist": false,
                        "PicklistItems": null,
                        "IsSendTime": false,
                        "DisplayOrder": 0,
                        "References": null,
                        "RelationshipName": null,
                        "Status": null,
                        "IsContextSpecific": false,
                        "Client": null,
                        "PartnerKey": null,
                        "PartnerProperties": null,
                        "CreatedDate": "0001-01-01T00:00:00.000",
                        "ModifiedDate": null,
                        "ID": 0,
                        "Owner": null,
                        "CorrelationID": null,
                        "ObjectState": null,
                        "IsPlatformObject": false
                    }
                ],
                "DataRetentionPeriod": "Days",
                "CategoryID": 0,
                "Status": null,
                "Client": {
                    "ID": 100000000,
                    "ClientID1": 0,
                    "PartnerClientKey": null,
                    "UserID": 0,
                    "PartnerUserKey": null,
                    "CreatedBy": 0,
                    "ModifiedBy": 0,
                    "EnterpriseID": 0,
                    "CustomerKey": null,
                    "CustomerID": null
                },
                "PartnerKey": null,
                "PartnerProperties": null,
                "CreatedDate": "0001-01-01T00:00:00.000",
                "ModifiedDate": null,
                "ID": 0,
                "ObjectID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
                "Owner": null,
                "CorrelationID": null,
                "ObjectState": null,
                "IsPlatformObject": false
            },
            "CreateResults": null,
            "ParentPropertyName": null,
            "StatusCode": "OK",
            "StatusMessage": "Data Extension created.",
            "OrdinalID": 0,
            "ErrorCode": 0,
            "RequestID": null,
            "ConversationID": null,
            "OverallStatusCode": null,
            "RequestType": "Synchronous",
            "ResultType": null,
            "ResultDetailXML": null
        }
    ]
}

Reference

Ressources and references related to the current methods.

Official documentation
SOAP object

Last Updated: