1
  • Can I process an SSJS AMPscript?
  • If I receive a string "AMPscript" can I give it a context and SSJS will process it?
  • in the provided code I am trying to take a string like this %%=FormatDate([transaction_datetime], 'dd/MM as','hh:mm')=%% and process it with the data obtained from the Data Extension
<script runat="server">
    Platform.Load("Core", "1.1.5");
    var externalKey = "EXTERNAL_KEY_DE";

    try {
        if (Request.Method == "POST") {
            var payload = Platform.Request.GetPostData("UTF-8");
            var jsonObject = Platform.Function.ParseJSON(payload);

            // String Ampscript
            var myString = jsonObject.ampscript;

            var deNotifications = DataExtension.Init(externalKey);
            var filter = {
                LeftOperand: {
                    Property: "notification_id",
                    SimpleOperator: "equals",
                    Value: jsonObject.notification_id
                },
                LogicalOperator: "AND",
                RightOperand: {
                    Property: "customer_id",
                    SimpleOperator: "equals",
                    Value: jsonObject.customer_id
                }
            }

            var columns = [
                "transaction_datetime",
                "transaction_placeofuse_name",
                "transaction_total_amount"
            ];

            var testeDE = deNotifications.Rows.Retrieve(filter, columns);

            if (testeDE.length > 0) {
                var notification = testeDE[0];

                var ampscriptContext = {
                    "transaction_datetime": notification.transaction_datetime,
                    "transaction_placeofuse_name": notification.transaction_placeofuse_name,
                    "transaction_total_amount": notification.transaction_total_amount
                };

                var ampscriptprocessed = Platform.Function.Execute(myString, ampscriptContext);

                Platform.Response.Write("AMPSCRIPT " + ampscriptprocessed);

            } else {
                Platform.Response.StatusCode = 404;
            }

            Platform.Response.Write("myString: " + testeDE.length);
        } else {
            Platform.Response.StatusCode = 400;
        }
    } catch (ex) {
        Platform.Response.StatusCode = 500;
    }
</script>
3
  • 1
    Where do you get this function Platform.Function.Execute from?
    – Duc Le
    Apr 6 at 2:17
  • 1
    Looks exactly as if ChatGPT created this "function" :) Jokes aside, @SpinXO what are you trying to achieve with your code? It is a bit unclear. You receive a JSON, and with that data, you filter the DE records to pull some values. What do you want to do next with those pulled values and AMPscript?
    – Niko
    Apr 6 at 7:30
  • @Niko I wanted this code to run the ampscript code. So that the FormatDate function works with the value obtained by DE.
    – SpinXO
    Apr 6 at 11:57

1 Answer 1

1

In order to run AMPscript within your SSJS code you simply need to wrap the code with Platform.Function.TreatAsContent function.

Example:

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

var transaction_datetime = "xxx" // some date that you would pull from the DE
var ampscriptProcessed = Platform.Function.TreatAsContent('%'+'%=FormatDate("' + transaction_datetime + '" , "dd/MM as","hh:mm")=%' + '%');

</script>

You must log in to answer this question.

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