SEND A TWILIO MESSAGE
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2025 10:50 AM
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
08-22-2025 10:39 PM
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
parametersobject 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. -
contentVariablesis 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);