0

We have a script activity in automation studio to update the text of a query definition and target a new data extension. It currently selects all fields using the standard SELECT *, but we need to now list out all the fields specifically. As it is currently (after a script runs to get a value - @jobno - from an ampscript block):

<script runat=server>
Platform.Load("Core","1");
var qd = QueryDefinition.Init("OurQueryName");
var deName = Variable.GetValue("@jobno");
var status = qd.Update({
    Name : "OurQueryName",
TargetUpdateType : "Overwrite",
TargetType : "DE",
Target : {
    Name : (deName),
    CustomerKey : (deName)
},
    QueryText : "SELECT * FROM StaticDataExtension WHERE Job_Ref = '".concat(deName).concat("'")
    });
qd.Perform();
</script>

This works perfectly but I need to basically loop through the fields in the DE, and stringify them, but my JS knowledge is patchy for how to loop and just stick them in a var (with a comma between them).

I'm using a loop elsewhere to retrieve DE fields using WSProxy for a DE duplication query:

<script runat="server">
Platform.Load('Core','1');
var i;
var fieldsFinal = []
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;
/**multiple more delete functions to delete irrelevant attributes**/
    fieldsFinal.push(fieldsRow);
}
</script>

This creates an array rather than a string, however the loop would give me exactly what I need - I'd just want to push the fieldsRow.Name for each row into a text string, separated by a comma - is this possible?

1 Answer 1

0

I would simply concatenate all the field names into a string through a loop, check if the field name contains any space characters, and add square brackets for that field name.

Here is what I have tried to retrieve all field names in a DE and then produce them into a string, separated by a comma.

<script runat="server">
Platform.Load("core", "1.1.1");

var DE_CustKey = "DE customer key";
var str_Fields = "";

var prox = new Script.Util.WSProxy();
var cols = ["Name", "FieldType", "DataExtension.CustomerKey"];

var data = prox.retrieve("DataExtensionField", cols, {Property: "DataExtension.CustomerKey", SimpleOperator: "equals", Value: DE_CustKey});
// Write(Stringify(data));

if (data.Results.length > 0) {
    for(var i = 0; i < data.Results.length; i++) {
        var fieldName = data.Results[i].Name;

        // If the field name has the space character then add square brackets
        if (fieldName.indexOf(" ") != -1) {
            fieldName = "[" + fieldName + "]";
        }

        // Process to produce a string
        if (i < (data.Results.length -1 ) ) {
            str_Fields += fieldName + ", ";
        } else {
            str_Fields += fieldName;
        }
    }
}
Write(str_Fields);
</script>

My output looks like this:

[Contact ID], Remark, ProductName

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .