0

Is there a way to dynamically generate multiple DE's based on an array? Something that might look like below? And, something that generates a new variable name (see line 6 & 27 should be the same values)? I'm not certain how to get lines 6 - 25 to repeat based on the number of values present in the "var name" array.

1  | <script runat=server>
2  | var prox = new Script.Util.WSProxy();
3  | var name = ["deRed","deGreen","deBlue"];
4  | var desc = ["descRed","descGreen","descBlue"];
5  |
6  | var [somethingDynamic] = {
7  | Name: name,
8  | CustomerKey: name,
9  | Description: desc,
10 | Fields: [{
11 |    FieldType: "Text",
12 |    Name: "SubscriberKey",
13 |    MaxLength: 254,
14 |    IsPrimaryKey: true,
15 |    IsNillable: false,
16 |    IsRequired: true
17 | },
18 | {
19 |    FieldType: "EmailAddress",
20 |    Name: "EmailAddress",
21 |    IsNillable: false,
22 |    IsRequired: true
23 | }],
24 | CategoryID: 123
25 | }
26 |
27 | var res = prox.createBatch("DataExtension", [somethingDynamic]);
28 | </script>

-- SFMC Documentation Reference: Create via WSProxy

0

You will need a for loop and utilise Array.prototype.push() to create your array of DE objects for the WSProxy batch call.

<script runat=server>
    Platform.Load("Core", "1");

    var prox = new Script.Util.WSProxy();
    var name = ["deRed", "deGreen", "deBlue"];
    var desc = ["descRed", "descGreen", "descBlue"];
    var objs = [];

    for (var i = 0; i < name.length; i++) {
        var somethingDynamic = {
            Name: name[i],
            CustomerKey: name[i],
            Description: desc[i],
            Fields: [{
                    FieldType: "Text",
                    Name: "SubscriberKey",
                    MaxLength: 254,
                    IsPrimaryKey: true,
                    IsNillable: false,
                    IsRequired: true
                },
                {
                    FieldType: "EmailAddress",
                    Name: "EmailAddress",
                    IsNillable: false,
                    IsRequired: true
                }
            ],
            CategoryID: 123
        }

        objs.push(somethingDynamic);
    }

    var res = prox.createBatch("DataExtension", objs);
</script>
  • Sorry @Josh do Blank. I think I accidentally edited your answer as it looked pretty identical to the one I was working on at the same time. My bad. – Macca Jul 11 at 0:16
  • All good. thanks @Macca. – Josh de Blank Jul 11 at 0:31
  • So, I put this in the script activity for Automation Studio. It validates and runs. It does not, however, place the DE's. I've double-checked the CategoryID and that was okay. Would you happen to know anything I could check? Since it's not throwing an error, I'm not sure where to start. @JoshdeBlank – Harley Jul 11 at 1:36
  • 1
    I've tested the code in a Cloudpage and it works as expected. Only item I changed was the category ID to one relevant to my instance. By testing in cloudpages you can see the returned result by using Write(Stringify(res)); after your proxy function call. – Josh de Blank Jul 11 at 3:25
0

I think you would benefit most from checking out some JS tutorials on iteration like this one from MDN.

Also, you wouldn't want to iterate over multiple arrays that contain related properties for the DE's you are trying to create. You should be using objects for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.