Created
January 1, 2026 19:24
-
-
Save ujjwaltiwari-ca/14c0b275099a8baf3cfd589a6d8c25d9 to your computer and use it in GitHub Desktop.
SFMC Server-Side JavaScript (SSJS) to authenticate with Marketing Cloud APIs and trigger SMS messages via the QueueMO QueueMO API, featuring automated logging and status updates in a Data Extension.
This file contains hidden or 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
| <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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment