Skip to content

Instantly share code, notes, and snippets.

@MyCueCards
Created July 18, 2023 05:08
Show Gist options
  • Save MyCueCards/dd627919a7843a2762a486e4ec87f769 to your computer and use it in GitHub Desktop.
Save MyCueCards/dd627919a7843a2762a486e4ec87f769 to your computer and use it in GitHub Desktop.
Adds multiple fields to a DE using SSJS. Loops through a DE to get the customer key.
// Name AND customer key of DE are: thisIsTheSourceDe
// Field in the above DE is: myCustomerKey (text)
// https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/ssjs_dataExtensionFieldsAdd.html
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
try {
// Define the customer key of the data extension that contains the 1000 customer keys
var dataExtensionCustomerKey = "thisIsTheSourceDe";
// Retrieve all rows from the data extension containing the customer keys
var dataExtension = DataExtension.Init(dataExtensionCustomerKey);
var data = dataExtension.Rows.Retrieve();
// Check if any rows were retrieved
if (data && data.length > 0) {
for (var i = 0; i < data.length; i++) {
var customerKey = data[i].myCustomerKey;
var fields = [
{
Name: "fieldnameOne",
CustomerKey: customerKey,
FieldType: "Text",
IsRequired: false,
MaxLength: 30
},
{
Name: "FieldNameTwo",
CustomerKey: customerKey,
FieldType: "Number",
IsRequired: false
}
];
var result = api.updateItem('DataExtension', {
"CustomerKey": customerKey,
"Fields": fields
});
Write("Updated fields for CustomerKey: " + customerKey + ", Result: " + Stringify(result) + "<br>");
}
} else {
Write("No records found in the data extension.");
}
} catch (error) {
Write("An error occurred: " + Stringify(error));
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment