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>

New contributor

Frankie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật