0

I am looking to pull the Auth token then use it to inject a record into an active Event Entry Journey. When I run the full code in Cloudpages, I get a record injected to the expected Journey. When I then put the code into a SMS Message, SFMC is unable to send the SMS through. Am I doing something wrong here? Do you have any idea if this is possible?

Other information:

  • The DE linked to the journey has all the fields in the @journeyBody variable, all fields are NOT NULLABLE.
  • Date fields are date data type, Mobile is phone data type while the rest are Text.
  • I have sent an SMS to myself with the Auth httpost section and managed to correctly generate the token which makes me think the httpost part for injecting a record in a journey can't be done

Here is my code:

%%[ 
SET @Contact_Key = _SubscriberKey
SET @MobilePhone = AttributeValue("MobilePhone")
SET @FirstName = AttributeValue("FirstName")
SET @AppointmentDate = AttributeValue("AppointmentDate")
SET @AppDateFormat = FORMATDATE(@AppointmentDate,"MMMM dd, yyyy h:mm")
SET @Case_ID = AttributeValue("Case_ID")
SET @Message_Assessment = "SFMC_Y"
SET @Inbound_or_Outbound = "Outbound"
SET @DateAddedAEST = Format(SystemDateToLocalDate(Now()),"yyyy/MM/dd h:mm:ss.tt")
SET @MessageText = Concat("[SFMC] Hi ",@FirstName,", please confirm you are free for your appointment on the following date: ",@AppDateFormat,". Reply to this SMS with a Yes or a No.")
SET  @body = '{"client_id": "EEEEEEEEEEEEEEEEEE","client_secret": "QQQQQQQQQQQQQQQQ", "grant_type": "client_credentials"}'
SET  @accessToken = HTTPPost2("https://RRRRRRRRRRRRRRRRR.auth.marketingcloudapis.com/v2/token", "application/json", @body, True, @callstatus, @response)
SET @PullaccessToken = REGEXMATCH(@callstatus,'^(?:.*"access_token":")(.*?)(?:".*)$',1)
SET @journeyBody = '{
    "ContactKey": @Contact_Key,
    "EventDefinitionKey": "APIEvent-SSSSSSSSSSSSSSSSSSSSSSS",
    "EstablishContactKey": true,
    "Data": { "Contact_Key": @Contact_Key, 
              "MobilePhone": @MobilePhone, 
              "FirstName":@FirstName, 
              "AppointmentDate": @AppointmentDate, 
              "Case_ID": @Case_ID, 
              "MessageText": @MessageText, 
              "Message_Assessment": @Message_Assessment, 
              "Inbound_or_Outbound": @Inbound_or_Outbound, 
              "DateAddedAEST": @DateAddedAEST
            }
   }'
SET @bearerAccess_token = CONCAT('Bearer ', @PullaccessToken)

HTTPPost2("https://RRRRRRRRRRRRRRRRR.rest.marketingcloudapis.com/interaction/v1/events", 'application/json', @journeyBody, True, @output,@respheader, 'Authorization', @bearerAccess_token)
]%%


%%=v(@MessageText)=%%

1 Answer 1

3

It looks like you have two issues here:

1) You are not sending values, just variable names

Your whole @journeyBody is a string and the variables there are not replaced by their values. You are literally sending this:

{
"ContactKey": @Contact_Key,
"EventDefinitionKey": "APIEvent-SSSSSSSSSSSSSSSSSSSSSSS",
"EstablishContactKey": true,
"Data": { "Contact_Key": @Contact_Key, 
            "MobilePhone": @MobilePhone, 10
            "FirstName":@FirstName, 
            "AppointmentDate": @AppointmentDate, 
            "Case_ID": @Case_ID, 
            "MessageText": @MessageText, 
            "Message_Assessment": @Message_Assessment, 
            "Inbound_or_Outbound": @Inbound_or_Outbound, 
            "DateAddedAEST": @DateAddedAEST
        }
}

You need to modify the code and build the @journeyBody using the Concat() function or by using Replace() to put the values in place of the stringified variable names.

The second way is lazy, but requires to just add this to your code after setting the @journeyBody:

SET @journeyBody = Replace(@journeyBody, '@Contact_Key', @Contact_Key) 
SET @journeyBody = Replace(@journeyBody, '@MobilePhone', @MobilePhone) 
SET @journeyBody = Replace(@journeyBody, '@FirstName', @FirstName) 
SET @journeyBody = Replace(@journeyBody, '@AppointmentDate', @AppointmentDate) 
SET @journeyBody = Replace(@journeyBody, '@Case_ID', @Case_ID) 
SET @journeyBody = Replace(@journeyBody, '@MessageText', @MessageText) 
SET @journeyBody = Replace(@journeyBody, '@Message_Assessment', @Message_Assessment) 
SET @journeyBody = Replace(@journeyBody, '@Inbound_or_Outbound', @Inbound_or_Outbound) 
SET @journeyBody = Replace(@journeyBody, '@DateAddedAE', @DateAddedAE) 

2) Strings need to be enclosed

In a proper JSON body, strings are enclosed with either single or double quotes. For example, if you go with the Replace method of building the JSON, strings would need to look like this:

`"ContactKey": "@Contact_Key",
(...)
"FirstName": "@FirstName", 
1
  • Thanks Rafal! You are an absolute legend! I am still getting use to AMPscript, I prefer using SSJS :P Take care mate
    – Weenus23
    Apr 22, 2022 at 3:06

You must log in to answer this question.

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