TransWikia.com

Marketing cloud API (POST /interaction/v1/events) - Multiple entries

Salesforce Asked by s3002068 on November 28, 2021

I am trying to inject contacts via journey rest api from cloud page.
It seems to work fine if it is unique record, but if the contact already exists in a journey, the cloud page does not throw an error but no record is added to the DE and subsequently, no entry in the journey.

I’ve tried 2 different scenarios:

  1. Subkey and Date : composite keys
  2. No primary key (Subkey and email address is not nullable)

Journey setting is to allow re-entry anytime.

Is rest api journey entry admitting only one unique entry (per subscriber) expected behavior?
If so, is there a workaround to allow multiple entries per subscriber?

Thanks in advance 🙂

One Answer

Perhaps you could try to add some more logic to your code, in which you first get a contact's exit status from a journey, if needed remove a contact from a journey, and then try to add him once again..

I wrote a quick sample:

function getStatus(token) {

    var config = {
        url : 'your rest base uri here' + "/interaction/v1/interactions/contactexit/status",
        contentType : "application/json",
        headerName : ["Authorization"],
        headerValue : ["Bearer " + token],
        payload : {
            ContactKey: 'pass in subscriber key',
            EventDefinitionKey: 'pass in key of journey here'
        }
    };

    var req = HTTP.Get(config.url, config.contentType, Stringify(config.payload), config.headerName, config.headerValue);
    
    var statusMessage = req[0].status[0].message;

    if (statusMessage == 'Exit process Completed.') {
        
        //make sure you delete contact from journey
        var deleteMessage = deleteContactFromJourney(token);

        //if everything went well you can add the contact to the journey.
        if (deleteMessage == 201) {
            addContactToJourney(token);
        } else {
            Write('error occured deleting contact.')
        }
        
    }
};

function deleteContactFromJourney(token) {

    var config = {
        url : 'your rest base uri here' + "/interaction/v1/interactions/contactexit",
        contentType : "application/json",
        headerName : ["Authorization"],
        headerValue : ["Bearer " + token],
        payload : {
            ContactKey: 'pass in subscriber key',
            EventDefinitionKey: 'pass in key of journey here'
        }
    };

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

        return req.Status;

    } catch (error) {
        return error;
    };


};

function addContactToJourney(token) {

    var config = {
        url : 'your rest base uri here' + "/interaction/v1/events",
        contentType : "application/json",
        headerName : ["Authorization"],
        headerValue : ["Bearer " + token],
        payload : {
            ContactKey: 'pass in subscriber key',
            EventDefinitionKey: 'pass in key of journey here',
            Data: {
                Attribute: "value"
            }
        }
    };

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

    } catch (error) {

        Write("Error occurred:" + Stringify(error));

    };
};

Please be aware that I haven't included in the logic of how to obtain access token. But since you already were able to fire the journey builder API from a cloud page, I assume that you already managed to deal with this.

I haven't tested this code, but it should give you a direction to look for.

Answered by Willem Ruys on November 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP