0

I'm trying to use server-side JS to dynamically write variables based on the column headers of a data extension for an email. Then fill those variables with the row contents of those field headers.

I was able to do so with the below sample code I found.

The only problem is that any personalization strings in that content gets written out as that string name (%%personalization_string%%).

I know with AMPScript to use TreatAsContent().

Is it possible to do this with SSJS? Thanks!

<script runat="server">

    Platform.Load("Core","1");
    var hidden_fields = "_CustomObjectKey,lookUpField";
    var DE = DataExtension.Init("DEContent");
    var data = DE.Rows.Lookup(["lookUpField"], [lookupValue]);

    if (data.length > 0) {
            Write("<table border='1' cellpadding='2' cellspacing='3' style='border-collapse:collapse;'>");
            Write("<tr>");
            for (var key in data[0]) {
                    if (hidden_fields.indexOf(key) == -1) {
                                    Write("<th>" + key.replace("_"," ") + "</th>");
                    }
            }
            Write("</tr>");
            for (var i=0;i<= data.length;i++) {
                    var d = data[i];
                    Write("<tr>");
                    for (var key in d) {
                                    if (hidden_fields.indexOf(key) == -1) {
                                                    Write("<td>" + d[key] + "</td>");
                                    }
                    }
                    Write("</tr>");
            }
            Write("</table>");

    }

</script>
1

Yes, this SFMC help page outlines how to do it. You can use the Variable.GetValue() function like this:

var pString = Variable.GetValue("personalization_string");
| improve this answer | |

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.