verified
Update a DataExtension fieldLearn how to update 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 field = {
"Name": "LastName",
"DefaultValue": "Doe",
"ObjectID": null
}
var fields = de.Fields.Retrieve();
for(var i in fields) {
if(fields[i].Name == field.Name) field.ObjectID = fields[i].ObjectID;
}
var attrs = {
CustomerKey: customerKey,
Fields: [field]
};
var result = de.Update(attrs);
<script runat="server">
Platform.Load("core", "1");
try {
var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var de = DataExtension.Init(customerKey);
var field = {
"Name": "LastName",
"DefaultValue": "Doe",
"ObjectID": null
}
var fields = de.Fields.Retrieve();
for(var i in fields) {
if(fields[i].Name == field.Name) field.ObjectID = fields[i].ObjectID;
}
var attrs = {
CustomerKey: customerKey,
Fields: [field]
};
var result = de.Update(attrs);
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
"OK"
WSProxy
var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var field = {
"Name": "LastName",
"DefaultValue": "Doe",
"ObjectID": null
}
var filter = {
LeftOperand: {
Property: "DataExtension.CustomerKey",
SimpleOperator: "equals",
Value: customerKey
},
LogicalOperator: "AND",
RightOperand: {
Property: "Name",
SimpleOperator: "equals",
Value: field.Name
}
}
var request = api.retrieve("DataExtensionField", ["ObjectID"], filter);
var objectId = request.Results[0].ObjectID;
field.ObjectID = objectId;
var result = api.updateItem("DataExtension", {
CustomerKey: customerKey,
Fields: [field]
});
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
try {
var customerKey = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var field = {
"Name": "LastName",
"DefaultValue": "Doe",
"ObjectID": null
}
var filter = {
LeftOperand: {
Property: "DataExtension.CustomerKey",
SimpleOperator: "equals",
Value: customerKey
},
LogicalOperator: "AND",
RightOperand: {
Property: "Name",
SimpleOperator: "equals",
Value: field.Name
}
}
var request = api.retrieve("DataExtensionField", ["ObjectID"], filter);
var objectId = request.Results[0].ObjectID;
field.ObjectID = objectId;
var result = api.updateItem("DataExtension", {
CustomerKey: customerKey,
Fields: [field]
});
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": null,
"Name": "LastName",
"ObjectID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"FieldType": "Text",
"IsPrimaryKey": false,
"MaxLength": 0,
"Ordinal": 0,
"DefaultValue": "Doe",
"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": 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