I found some useful documentation/past examples of using wsproxy to copy and create a duplicate data extension.
Goal: I would like to copy a DE from a source business unit and then create a DE with the same structure in a target business unit under the same parent account.
Issue: The code works when it is contained within the same business (i.e. data to be copied and the target folder are all within the same BU. The issue occurs when I try to use pull from one business unit and create it in another. I tried adding the code (retrieveprox.setClientId({ "ID": xxx, "UserID": yyy});) before calling the retrieve and create scripts, but it returns a 500 internal error.
The documentation on "setClientId({ "ID": xxx, "UserID": yyy})" is very lacking, so maybe I am making a mistake here. How can I be sure I am using the right values for both ID and UserID?
Code:
<script runat="server">
/******* Retrieves Fields *******/
var retrieveprox = new Script.Util.WSProxy();
retrieveprox.setClientId({ "ID": xxx, "UserID": yyy});
var cols2 = ["Client.ID","CustomerKey","DefaultValue","FieldType","IsPrimaryKey","IsRequired","MaxLength","ModifiedDate","Name","ObjectID","Ordinal","Scale"];
var filter2 = {
Property: "DataExtension.CustomerKey",
SimpleOperator: "equals",
Value: "8ACA6905-30E3-45F1-BE16-287D45F8F057"
};
var fields = retrieveprox.retrieve("DataExtensionField", cols2, filter2); /* WSProxy Retrieve */
/* Results of the Retreive */
var fieldsResults = fields.Results;
var i;
var fieldsFinal = []
/******* For loop to weed out some of the troublesome props in Results Object ******/
for (i=0;i<fieldsResults.length;i++) {
var fieldsRow = fields.Results[i];
delete fieldsRow.ObjectID;
delete fieldsRow.StorageType;
if (fieldsRow.FieldType != "Text") {
delete fieldsRow.MaxLength;
}
delete fieldsRow.DataExtension;
delete fieldsRow.PartnerMap;
delete fieldsRow.AttributeMaps;
delete fieldsRow.Markups;
delete fieldsRow.Precision;
delete fieldsRow.Scale;
delete fieldsRow.PicklistItems;
delete fieldsRow.References;
delete fieldsRow.Client;
delete fieldsRow.PartnerKey;
delete fieldsRow.PartnerProperties;
delete fieldsRow.CreatedDate;
delete fieldsRow.ModifiedDate;
delete fieldsRow.ID;
fieldsFinal.push(fieldsRow);
}
/* Create DE */
var proxCreate = new Script.Util.WSProxy();
proxcreate.setClientId({ "ID": xxxx, "UserID": yyyy});
var customerKey = "testDE_7";
var name = "testDE_7";
var description = "This is a test DE made in WSProxy"
var folder = 761
var de = {
Name: name,
CustomerKey: customerKey,
Description: description,
Fields: fieldsFinal,
CategoryID: folder
}
var create = proxCreate.createItem("DataExtension", de);
</script>