Businesses must send instant notifications to customers for real-time updates or personalized services using WhatsApp. Salesforce provides functionality to send WhatsApp messages using Social Studio. If we don’t have a Social Studio license then we can utilise Twilio API to send WhatsApp messages. This post will provide complete details about sending WhatsApp messages from the Lightning Application by Integrating Salesforce and WhatsApp using Twilio API.
Steps for Twilio API integration in Salesforce:
- Signup in Twilio
- Create Sandbox environment
- Create Apex for consuming API
- Create Lightning component
1. Signup in Twilio
Sign up in Twilio to integrate it with the Salesforce application.
It will send one verification email to verify the email. Verify email to continue. This will open the below screen to verify the phone number.
Verify mobile/phone number to process further. After verification and confirming the required information, this popup will show. Confirm this also as it is for developers to test Whatsapp messaging.
2. Create Sandbox environment
Developers can test WhatsApp messaging using the Sandbox environment. Setting the Sandbox environment is 4 steps process and it will start after the above popup is confirmed.
The first step is to connect the sandbox environment with your Whatsapp number. So send the message ‘join <connection code>’ to +14155238886. Connection code will be generated automatically and it will be visible on pages like in below page with bold characters. For my test environment, it is difficult-now.
Once you send a proper message, it will link your WhatsApp number to the sandbox environment.
In the next step, send any message from the sandbox environment to your WhatsApp number. You can select any option to test like ‘Appointment Reminder’, ‘Order Notifications’ and ‘Verification Codes’.
The below screen shows, how the message will be delivered to your number.
After confirming that the message has been delivered to your WhatsApp number, you can proceed to the next step. The next step is to send a message from your WhatsApp number to verify two-way communication.
After successful communication, it will show the sandbox environment detail. For testing, we can only use 3 highlighted message formats.
Now our sandbox environment is set up. Let us proceed with using Twilio API in Apex.
3. Create Apex for consuming API
Integrate Twilio API to send WhatsApp messages using Apex. Apex needs your Twilio account SID and secret token. You can get this information here. If it is not available by default then click Show API Credentials on the right side of the page.
Add Twilio API Url in Remote Site Setting.
Sending messages will require three parameters.
- From: This is the sender’s WhatsApp number. For the sandbox environment, this number will be +14155238886.
- To: This is the receiver’s WhatsApp number. Use the above WhatsApp number which you have configured in a sandbox environment.
- Body: This is the message which will be sent. For testing use the format which is mentioned on the sandbox page.
4. Create a Lightning component
Create a lightning component to send messages using the above apex class.
Provide the receiver’s WhatsApp number and message in a valid format. It will send a WhatsApp message to the provided number.
If you need to send WhatsApp message to other mobile number then it should be verified first. You can verify mobile no here.
Reference: https://www.twilio.com/docs/sms/whatsapp
Other Useful Posts
- Implementing Apex Cursors for Optimal Resource Management in Salesforce
- Efficient Ways to Debug Salesforce Platform Events
- How to Use Image Cropper in Salesforce Lightning Web Component
- Appointment Assistance Real Time Location Url Generation
- Setting Up Live Chat with Salesforce Messaging
- Top Developer Features in Salesforce Spring ’24 Release
31 Comments
Hi Dhanik, in apex controller, name of method should be SendMessage i guess. can you please confirm
Thanks
Yes Sakshi. Thank you for informing. I have updated it.
how to receive message/incoming message from whatsapp
Hello Sumit,
Twilio has both functionality to receive and send SMS. Refer link
Thank You,
Dhanik
Hi Dhannik,
Your post is very nice but i need some help. i want show incoming message from user devive in saleforce component so how to receive message/incoming message from whatsapp
Hello Sumit,
Twilio has both functionality to receive and send SMS. Refer link
Thank You,
Dhanik
Hi Dhanik,
Please, Can you help me in Received messages storing in database of salesforce.
Thank you
Hello Basavaraj,
You can use any Custome Object to store messages. Still, if you need to discuss then ping me on Linkedin or telegram.
Thank You,
Dhanik
does this requires twilio for salesforce appexchange product installed to access twilio outbound api call or its public api. Thanks for the post.
Hello Kumar,
It is accessing twilio api. No need to install App-exchange product.
Thank You,
Dhanik
Hello,
I want to give it a shot, will it work with a developer org?
Yes, It will work for developer org.
Thank You,
Dhanik
Hi Dhanik ,
I am getting below error when I am trying to send SMS via apex using above code .
{“code”: 21211, “message”: “The ‘To’ number Wtatsapp:+91XXXXXXXXXX is not a valid phone number.”, “more_info”: “https://www.twilio.com/docs/errors/21211”, “status”: 400}
Can you confirm if I am missing anything?
A quick response will be highly appreciated.
Hello Manav,
You have to verify the mobile number where you are sending SMS. Let me know if this still not work. We can connect and resolve your issue.
Thank You,
Dhanik
Thanks Dhanik for guiding me, its working fine now
Hello Dhanik,
I am trying to get all messages from Twilio to salesforce. So i used ‘GET’ method for it, but in response Twilio only send the latest single message. Can you please help me out?
Hello Harshal,
Please check https://www.twilio.com/docs/sms/api/message-resource#read-multiple-message-resources for getting all messages.
Thank you,
Dhanik
Hi Dhanik,
How to send multimedia messages
Hello Ifthikar,
Checkout this page from Twilio for MMS support.
Thank You,
Dhanik
Pingback: Generate OTP in LWC | SalesforceCodex
I am getting an error
18:57:04:772 USER_DEBUG [29]|DEBUG|{“code”: 63007, “message”: “Twilio could not find a Channel with the specified From address”, “more_info”: “https://www.twilio.com/docs/errors/63007”, “status”: 400}
18:57:04:773 USER_DEBUG [35]|DEBUG|Twilio errorTwilio could not find a Channel with the specified From address
And Can you Please tell me In Status Callback URL Which URL I Should used
Hey Akash,
Please check this post
Thank You,
Dhanik
Hi ,
Hi Dhanik ,
I am getting below error and also verified my number as well.
{“code”: 63007, “message”: “Twilio could not find a Channel with the specified From address”, “more_info”: “https://www.twilio.com/docs/errors/63007”, “status”: 400}
Can you please help me out? Thanks!
Hello Pramod,
Please check this blog https://support.twilio.com/hc/en-us/articles/360009017394-Troubleshooting-the-Twilio-Sandbox-for-WhatsApp#:~:text=Error%3A%20Twilio%20could%20not%20find,message%20is%20not%20formatted%20correctly.
Thank You,
Dhanik
Hi Dhanik,
can you please share the get message code and aura modification for this
Hello Kannan,
You can include this LWC in your aura as well. Any specific reason, only to create in Aura component?
Thank You,
Dhanik
Hi Dhanik,
To connect sandbox with my number, it should show join . In my case, it’s only showing join not sandbox name to send a message. I tried sending join but there is no sandbox name, do you have any idea why my sandbox name not coming.
Thanks
Hello Ankit,
Try to create a new user and connect. I hope, it will resolve. Otherwise, we can connect to resolve your issue.
Thank You,
Dhanik
Pingback: Difference between SOAP and REST API? - SalesforceCodex
Please help me to send a WhatsApp message to other mobile numbers or how to add mobile numbers in Twilio.
Hello Samir,
What issue you are facing. Please let me know so that i can help you. You can also refer video which is covering your issue as well.
Thank You,
Dhanik