Last active
July 16, 2024 20:09
-
-
Save saamerm/4a18c5a33aabc59b0121d023ed0605e1 to your computer and use it in GitHub Desktop.
Perform an Salesforce Marketing Cloud Post API call, then wait 2 seconds and take the response to Get the status of the email sent and Print, while using multiple bodies within your powershelgl code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Invoke-EmailAPI { | |
param ( | |
[hashtable]$body | |
) | |
# Define the API endpoint | |
$url = "{URL}" | |
# Define the authorization token | |
$token = "afasfa23414" | |
# Convert the body to JSON | |
$jsonBody = $body | ConvertTo-Json -Depth 10 | |
# Create the headers | |
$headers = @{ | |
Authorization = "Bearer $token" | |
} | |
# Perform the POST request | |
$response = Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $jsonBody -ContentType "application/json" | |
# Output the response | |
$response | ConvertTo-Json | |
# Capture the messageKey from the response | |
$messageKey = $response.responses[0].messageKey | |
# Takes some time to get the accurate email send status, so we add a 2 second delay | |
Start-Sleep -Seconds 2 | |
# Define the second API endpoint using the messageKey | |
$secondUrl = "$url/$messageKey" | |
# Perform the second GET request | |
$secondResponse = Invoke-RestMethod -Uri $secondUrl -Headers $headers -Method Get | |
# Output the response | |
$secondResponse | ConvertTo-Json | |
} | |
# Define the JSON bodies | |
$bodies = @( | |
@{ | |
definitionKey = "BOOKING_CONFIRMATION" | |
recipients = @( | |
@{ | |
contactKey = "[email protected]" | |
to = "[email protected]" | |
attributes = @{ | |
checkInTime = "02:00:00 PM" | |
checkOutTime = "01:00:00 PM" | |
packageInfo = "Six long stemmed strawberries dipped in white and dark chocolate,Enjoy a bouquet of seasonal flowers upon your arrival" | |
packageTotalWithTax = "70" | |
totalCostWithTax = "1150" | |
NightsQty = "2" | |
AdultQty = "2" | |
ChildQty = "4" | |
roomName = "D2DSC" | |
dailyRate = "445.23" | |
GteCxlPol = "Guarantee & Cancellation Policy" | |
guestId = "" | |
hotelId = "26782" | |
email = "[email protected]" | |
CFirstName = "M" | |
CLastName = "B" | |
ConfirmNo = "BOSTPNR0001" | |
ArrivalDt = "07/12/2024" | |
DepartDt = "07/13/2024" | |
rateCode = "" | |
} | |
} | |
) | |
}, | |
@{ | |
definitionKey = "BOOKING_CONFIRMATION" | |
recipients = @( | |
@{ | |
contactKey = "[email protected]" | |
to = "[email protected]" | |
attributes = @{ | |
checkInTime = "02:00:00 PM" | |
checkOutTime = "01:00:00 PM" | |
packageInfo = "Six long stemmed strawberries dipped in white and dark chocolate,Enjoy a bouquet of seasonal flowers upon your arrival" | |
packageTotalWithTax = "70" | |
totalCostWithTax = "1250" | |
NightsQty = "2" | |
AdultQty = "2" | |
ChildQty = "4" | |
roomName = "D2DSC" | |
dailyRate = "442.12" | |
GteCxlPol = "Guarantee & Cancellation Policy" | |
guestId = "" | |
hotelId = "26782" | |
email = "[email protected]" | |
CFirstName = "M" | |
CLastName = "B" | |
ConfirmNo = "BOSTPNR0001" | |
ArrivalDt = "07/12/2024" | |
DepartDt = "07/13/2024" | |
rateCode = "" | |
} | |
} | |
) | |
} | |
) | |
# Loop through each body and make the API calls | |
foreach ($body in $bodies) { | |
Invoke-EmailAPI -body $body | |
} | |
# ----------------------------- V1 of the code above that explains whats happening above in a simple way | |
# # Define the API endpoint | |
# $url = "{}" | |
# # Define the JSON body | |
# $body = @{ | |
# definitionKey = "BOOKING_CONFIRMATION" | |
# recipients = @( | |
# @{ | |
# contactKey = "[email protected]" | |
# to = "[email protected]" | |
# attributes = @{ | |
# checkInTime = "02:00:00 PM" | |
# checkOutTime = "01:00:00 PM" | |
# packageInfo = "Six long stemmed strawberries dipped in white and dark chocolate,Enjoy a bouquet of seasonal flowers upon your arrival" | |
# packageTotalWithTax = "70" | |
# totalCostWithTax = "1150" | |
# NightsQty = "2" | |
# AdultQty = "2" | |
# ChildQty = "4" | |
# roomName = "D2DSC" | |
# dailyRate = "You got this for free" | |
# GteCxlPol = "Guarantee & Cancellation Policy" | |
# guestId = "" | |
# hotelId = "26782" | |
# email = "[email protected]" | |
# CFirstName = "M" | |
# CLastName = "B" | |
# ConfirmNo = "BOSTPNR0001" | |
# ArrivalDt = "07/12/2024" | |
# DepartDt = "07/13/2024" | |
# rateCode = "" | |
# } | |
# } | |
# ) | |
# } | ConvertTo-Json -Depth 10 | |
# # Define the authorization token | |
# $token = "afasfa23414" | |
# # Create the headers | |
# $headers = @{ | |
# Authorization = "Bearer $token" | |
# } | |
# # Perform the POST request | |
# $response = Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body -ContentType "application/json" | |
# # Output the response | |
# $response | ConvertTo-Json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment