0

I'm using JavaScript on a CloudPage to trigger a journey entry using the REST API method interaction/v1/events. One of the contact data elements I want to pass in to the journey is a date value for when the request was processed. To set this variable in Javascript I am using the following:

  Platform.Load("Core","1.1.1");
  var payloadDatetime = DateTime.SystemDateToLocalDate(Now());

This generates a JavaScript date object, with a value like "Thu, 27 Aug 2020 12:07:13 GMT-06:00". This is incorrect as the timezone has not be changed to the local value - it remains at server time. I can live with this though.

The main issue I have is that when trying to trigger the journey I get the following error message: "Field 'payloadDatetime' value 'Thu, 27 Aug 2020 12:07:13 GMT-06:00' does not match type 'Date'\r\n". The request body (built by concatenating JavaScript variables) looks like this:

{
    "ContactKey": "my contact key",
    "EventDefinitionKey": "APIEvent-XXXX",
    "Data": {
        "payloadDatetime": "Thu, 27 Aug 2020 12:07:13 GMT-06:00"
    }
}

Manually changing the date value to "27 Aug 2020 12:07:13" allows the call to be accepted.

I would expect to use the toJSON() method to get a more friendly date string, but unfortunately as this was introduced with ECMAScript 5, it is unsupported by SFMC SSJS which uses ECMAScript 3.

Is there any way to manipulate the JavaScript date format to make it compatible with the REST API?

2
  • What about using toiSOString()? I'm not super-familiar with CloudPage tech, but that would be the first thing I'd try. – sfdcfox Aug 27 '20 at 12:31
  • @sfdcfox - Thanks for the tip, but unfortunately that method doesn't work either, despite it being part of ECMAScript 3. It causes a 500 error when executing the script. – SteveS Aug 27 '20 at 12:47
1

With the APIs and Marketing Cloud more generally, it's safest to stick to ISO 8601 or ODBC Canonical formats. You could either take the lazy approach of piggy-backing off of AMPScript's built in date formatting functions or write your own function to format your dates:

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

    // Lazy approach using embedded AMPScript
    var payloadDatetime = Platform.Function.TreatAsContent("%" + "%=Format(SystemDateToLocalDate(NOW()),'yyyy-MM-ddTHH:mm:ss')=%" + "%");

    // Using your own toISOString function
    payloadDatetime = toISOString(Platform.Function.SystemDateToLocalDate(new Date()));

    function toISOString(d){
            return  [   d.getFullYear(),
                        padLeft(d.getMonth()+1),
                        padLeft(d.getDate())
                    ].join('-')+'T'+
                    [   padLeft(d.getHours()),
                        padLeft(d.getMinutes()),
                        padLeft(d.getSeconds())
                    ].join(':');
    }

    function padLeft(num){
            var len = String(num).length;
            return len == 1? '0' + num: num;
    }
</script>

If your happy with the time in CST, an even lazier approach would be to just make your "payloadDatetime" field in your Data Extension nullable and default it to the current date. Then, just don't bother providing the field in your payload at all and let SFMC populate the field as it goes.

1
  • Thanks @Macca - that second solution is perfect for me. The final option (DE field default value) isn't suitable as I need to have the same date logged in several DEs to enable data comparison. When the email sent in the journey is sent it logs data to another DE, and I need to use the contact data version, not the current date time value. – SteveS Aug 28 '20 at 12:55

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.