Frankie
Frankie

Reputation: 1

Trigger Salesforce Marketing Cloud API Event on form submit

I am using Salesforce Marketing Cloud to send emails. I want to use the API Event in journey builder, I have tested the API using postman and the following code below and the journey is working fine, and the email is sending.

The issue I am facing is the following code triggers the API on page load and for a static subscriber. What I want to happen is to create a form so that when a user submits the SSJS will create a token and fire the API event.

I think this can be done with SSJS, but you cant mix SSJS and JS in the same script tag apparently.


%%[

SET @SubscriberKey = "0x00000001"
SET @EmailAddress = "[email protected]"
SET @PostalCode = "2000"

]%%

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]>      <html class="no-js"> <!--<![endif]-->
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="">
    </head>
    <body>
    <form id="myForm">
        <label for="name">Name:</label><br>
        <input type="text" id="name" name="name" required><br><br>
        <label for="email">Email:</label><br>
        <input type="email" id="email" name="email" required><br><br>
        <button type="submit">Submit</button>
    </form>
    </body>
    <script runat="server">

        Platform.Load("Core", "1.1.1");

        var data = {
            SubscriberKey: Variable.GetValue("@SubscriberKey"),
            EmailAddress: Variable.GetValue("@EmailAddress"),
            PostalCode: Variable.GetValue("@PostalCode")
        }

        var setup = {
            authBaseURI: "https://xxxxxxxxxxxxxxxxxxxx-xxxxxxx.auth.marketingcloudapis.com/",
            restBaseURI: "https://xxxxxxxxxxxxxxxxxxxx-xxxxxxx.rest.marketingcloudapis.com/",
            clientId: "xxxxxxxxxxxxxxxxxxxxxxxx",
            clientSecret: "xxxxxxxxxxxxxxxxxxxxxxxx",
            eventDefinitionKey: "APIEvent-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
        }

        try {
            var token = getToken(setup);
            var success = false;
            if (!!token) success = triggerEvent(token, setup, data);
            if (!!success) Write("Subscriber was successfully injected into the Journey");
            else Write("Failed to inject subscriber into the Journey");
        } catch (err) {
            Write("Error: " + Stringify(err));
        }

        function getToken(setup) {

            var config = {
                url: setup.authBaseURI + "v2/token",
                contentType: "application/json",
                payload: {
                    "client_id": setup.clientId,
                    "client_secret": setup.clientSecret,
                    "grant_type": "client_credentials"
                }
            }

            var req = HTTP.Post(config.url, config.contentType, Stringify(config.payload));

            if (req.StatusCode == 200) {
                var res = Platform.Function.ParseJSON(req.Response[0]);
                return res.access_token;
            } else {
                return false;
            }

        }

        function triggerEvent(token, setup, data) {

            var config = {
                url: setup.restBaseURI + "interaction/v1/events",
                contentType: "application/json",
                headerName: ["Authorization"],
                headerValue: ["Bearer " + token],
                payload: {
                    ContactKey: data.SubscriberKey,
                    EventDefinitionKey: setup.eventDefinitionKey,
                    Data: data
                }
            }

            var req = HTTP.Post(config.url, config.contentType, Stringify(config.payload), config.headerName, config.headerValue);

            if (req.StatusCode == 201) {
                var res = Platform.Function.ParseJSON(req["Response"][0]);
                if (res.eventInstanceId != null && res.eventInstanceId != "") return true;
            } else {
                return false;
            }

        }

    </script>
</html>

Upvotes: 0

Views: 166

Answers (0)

Related Questions