Lab note
Sample Code for Sending SMS Messages and Logging Responses in SFMC Using QueueMO API
SFMC architect · technical writer · 3 min read
- SFMC SMS Using QueueMO
- QueueMO
- SMS
Introduction
In this tutorial, we will walk through the process of sending SMS messages to mobile numbers stored in a data extension and logging the responses in another data extension using Salesforce Marketing Cloud (SFMC). We will be using the QueueMO API for sending SMS messages.
Step 1: Create Data Extensions
1.1 Data Extension for Mobile Numbers
First, we need a data extension to store the mobile numbers. This data extension should have the following fields:
- mobile_number (Text, Primary Key)
- subscriberkey (Text)
- Processed (Boolean)
Example Configuration:
Data Extension Name: SMS_Mobile_Numbers
Fields: mobile_number, subscriberkey, Processed
1.2 Data Extension for Logging Responses
Next, we need a data extension to log the responses from the SMS messages:
- subscriberkey (Text)
- mobile_number (Text)
- response (Text)
- timestamp (Date)
Example Configuration:
Data Extension Name: SMS_Response_Log
Fields: subscriberkey, mobile_number, response, timestamp
Step 2: Write the Script
Below is the complete SSJS script to handle the authentication and API call. You can host this in Automation Studio as a Script Activity.
<script runat="server">
Platform.Load("Core", "1");
// Function to refresh token
function refreshToken() {
var url = 'https://your-auth-url.auth.marketingcloudapis.com/v2/token';
var payload = {
"client_id": "your-client-id",
"client_secret": "your-client-secret",
"grant_type": "client_credentials"
};
var response = HTTP.Post(url, "application/json", Stringify(payload));
var tokenResponse = Platform.Function.ParseJSON(response.Response[0]);
Write("Token Response: " + Stringify(tokenResponse));
return "Bearer " + tokenResponse.access_token;
}
// Function to send SMS using QueueMO API
function sendSMS(mobileNumber, token) {
var url = "https://your-rest-url.rest.marketingcloudapis.com/sms/v1/queueMO/";
var payload = {
"mobileNumbers": [mobileNumber],
"shortCode": "your-shortcode",
"messageText": "your-message-text"
};
var headerNames = ["Authorization"];
var headerValues = [token];
var response = HTTP.Post(url, "application/json", Stringify(payload), headerNames, headerValues);
Write("SMS Response: " + Stringify(response));
Write("SMS Response Status Code: " + response.StatusCode);
Write("SMS Response Headers: " + Stringify(response.Headers));
Write("SMS Response Body: " + response.Response[0]);
return response;
}
// Refresh token once at the beginning
var token;
try {
token = refreshToken();
Write("Token refreshed successfully: " + token);
} catch (e) {
Write("Error refreshing token: " + Stringify(e));
}
// Retrieve mobile numbers from data extension where Processed is false
var contacts = DataExtension.Init("SMS_Mobile_Numbers").Rows.Lookup(["Processed"], [false]);
var contactCount = contacts.length;
Write("Number of contacts retrieved: " + contactCount);
// Log Data Extension
var logDE = DataExtension.Init("SMS_Response_Log");
for (var i = 0; i < contactCount; i++) {
var contact = contacts[i];
var mobile_number = contact.mobile_number;
var subscriberkey = contact.subscriberkey;
var response;
Write("Processing contact: " + mobile_number);
try {
response = sendSMS(mobile_number, token);
Write("SMS sent to " + mobile_number + ". Response: " + Stringify(response));
} catch (e) {
Write("Error sending SMS to " + mobile_number + ": " + Stringify(e));
response = {
"error": Stringify(e)
};
}
// Update the Processed field to True
try {
DataExtension.Init("SMS_Mobile_Numbers").Rows.Update({ "Processed": true }, ["mobile_number"], [mobile_number]);
Write("Updated Processed field for " + mobile_number);
} catch (e) {
Write("Error updating Processed field for " + mobile_number + ": " + Stringify(e));
}
// Log the attempt
try {
logDE.Rows.Add({
"subscriberkey": subscriberkey,
"mobile_number": mobile_number,
"response": Stringify(response),
"timestamp": Platform.Function.Now()
});
Write("Logged attempt for " + mobile_number);
} catch (e) {
Write("Error logging attempt for " + mobile_number + ": " + Stringify(e));
}
}
</script>
Replace your-auth-url, your-client-id, your-client-secret, your-rest-url, your-shortcode, and your-message-text with your Business Unit values before running.
Conclusion
By following these steps, you can set up an automated system in SFMC to send and log SMS messages efficiently. Feel free to customize the placeholders in the script to fit your specific REST credentials and Business Unit requirements.
Stay updated
Get new posts by email
SFMC how-tos, build stories, and architecture notes—delivered when they publish. RSS works too, but most people prefer email.
One email when a post goes live. Unsubscribe anytime.