SEND A TWILIO MESSAGE

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Dear community, I need help in sending a Twilio Message from ServiceNow. In this particular case I need to send info via WhatsApp Business. Everything is set up, but, I have NO variables changing on my receiving message. Does someone know what could it be?
Here my SNOW CODE:
var parameters = {
"1": caseNumber,
"2": description,
"3": caseOpened,
"5": host,
"6": ip,
"7": status,
"8": type
};
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod("POST");
request.setEndpoint('https://studio.twilio.com/v2/Flows/{flowSid}/Executions');
request.setBasicAuth(accountSid, authToken);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var payload = "From=" + encodeURIComponent(from) +
"&To=" + encodeURIComponent(to) +
"&contentVariables=" + encodeURIComponent(JSON.stringify(parameters));
request.setRequestBody(payload);
var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.info("Status HTTP: " + httpStatus);
gs.info("Resposta do Twilio: " + responseBody);
And here is my TEMPLATE ON TWILIO:
A new issue is found as described bellow:
CASE: {{1}}
{{2}}
DATE: {{3}}
NODE INFO
Host: {{4}}
IP: {{5}}
STATUS: {{6}}
TYPE: {{7}}
For more information check your Servicenow Instance
It's my understamd that whan this payload gets there the {{1}}, {{2}}, {{3}} will be replaced by the values corresponding the keys on the payload. What am I doing wrong?
Could anyone help me out?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi @BRUNO BANDEIRA
The Issue is in your code you are building the payload as:
var parameters = {
"1": caseNumber,
"2": description,
"3": caseOpened,
"5": host,
"6": ip,
"7": status,
"8": type
};
But in your Twilio Studio template, you’re referencing:
CASE: {{1}} {{2}}
DATE: {{3}}
NODE INFO Host: {{4}} <--- (this one is missing in your parameters object!)
IP: {{5}}
STATUS: {{6}}
TYPE: {{7}}
Notice the mismatch:
-
Your payload has keys
"1"…"3", "5"…"8"
. -
Template expects
{{4}}
as well. -
You skipped
"4"
in your parameters, so Twilio cannot resolve{{4}}
(and often, if one breaks, all substitutions fail).
Fix
-
Make sure all placeholders used in the template exist in the payload and align perfectly with the numbering.
-
Update your
parameters
object like this:
var parameters = {
"1": caseNumber,
"2": description,
"3": caseOpened,
"4": host, // <--- you missed this one
"5": ip,
"6": status,
"7": type
};
Payload Encoding
Your body is correct in principle:
var payload = "From=" + encodeURIComponent(from)
+ "&To=" + encodeURIComponent(to)
+ "&Parameters=" + encodeURIComponent(JSON.stringify(parameters));
But:
-
In Twilio Studio Execution API, the parameter is
Parameters
(capital P), notcontentVariables
. -
contentVariables
is used with Messaging Service Templates (for WhatsApp pre-approved templates), not Studio Flows.
So change:
"&contentVariables=" + encodeURIComponent(JSON.stringify(parameters));
to:"&Parameters=" + encodeURIComponent(JSON.stringify(parameters));
-----------------------------------------------------------------------------
Corrected Code
var parameters = {
"1": caseNumber,
"2": description,
"3": caseOpened,
"4": host,
"5": ip,
"6": status,
"7": type
};
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod("POST");
request.setEndpoint('https://studio.twilio.com/v2/Flows/{flowSid}/Executions');
request.setBasicAuth(accountSid, authToken);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var payload = "From=" + encodeURIComponent(from)
+ "&To=" + encodeURIComponent(to)
+ "&Parameters=" + encodeURIComponent(JSON.stringify(parameters));
request.setRequestBody(payload);
var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.info("Status HTTP: " + httpStatus);
gs.info("Resposta do Twilio: " + responseBody);
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
YouTube: https://www.youtube.com/@learnservicenowwithravi
LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/