Add a DataExtension field verified

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

Core

var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";

var de = DataExtension.Init(customerKey);

var newField = {
    Name : "MyNewField",
    CustomerKey : GUID(),
    FieldType : "Number",
    IsRequired: true,
    DefaultValue: "100"
};

var result = de.Fields.Add(newField);
<script runat="server">

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

	try {

        var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";

        var de = DataExtension.Init(customerKey);

        var newField = {
            Name : "MyNewField",
            CustomerKey : GUID(),
            FieldType : "Number",
            IsRequired: true,
            DefaultValue: "100"
        };

        var result = de.Fields.Add(newField);

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

</script>
"OK"

WSProxy

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

var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";

var fields = [
    {
        Name : "MyNewField",
        CustomerKey : GUID(),
        FieldType : "Number",
        IsRequired: true,
        DefaultValue: "100"
    }
];

var result = api.updateItem('DataExtension', {
	"CustomerKey": customerKey,
	"Fields": fields
});
<script runat="server">

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

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

        var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";

        var fields = [
            {
                Name : "MyNewField",
                CustomerKey : GUID(),
                FieldType : "Number",
                IsRequired: true,
                DefaultValue: "100"
            }
        ];

        var result = api.updateItem('DataExtension', {
            "CustomerKey": customerKey,
            "Fields": fields
        });

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

</script>
{
    "Status": "OK",
    "RequestID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
    "Results": [
        {
            "Object": {
                "CustomerKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
                "Name": null,
                "Description": null,
                "IsSendable": false,
                "IsTestable": false,
                "SendableDataExtensionField": null,
                "SendableSubscriberField": null,
                "Template": null,
                "DataRetentionPeriodLength": 0,
                "DataRetentionPeriodUnitOfMeasure": 0,
                "RowBasedRetention": false,
                "ResetRetentionPeriodOnImport": false,
                "DeleteAtEndOfRetentionPeriod": false,
                "RetainUntil": null,
                "Fields": [
                    {
                        "CustomerKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
                        "Name": "MyNewField",
                        "ObjectID": null,
                        "FieldType": "Number",
                        "IsPrimaryKey": false,
                        "MaxLength": 0,
                        "Ordinal": 0,
                        "DefaultValue": "100",
                        "DataExtension": null,
                        "StorageType": "Unspecified",
                        "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
                    }
                ],
                "DataRetentionPeriod": "Days",
                "CategoryID": 0,
                "Status": null,
                "Client": 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
            },
            "UpdateResults": null,
            "ParentPropertyName": null,
            "StatusCode": "OK",
            "StatusMessage": "Data Extension updated.",
            "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
Data Extension Fields
SOAP object

Help me turn coffee into code

This website is provided to you free of charge. However, a lot of time and effort are spent to write, test and mainain the code. Please consider supporting my work by buying me a cup of coffee.

Last Updated: