Business Requirement
We often get requirements from businesses to send SMS from Dynamics CRM to customer on certain actions.
- Send SMS when New Lead created to Customer.
- Send SMS when Status of Case is changed.
- Send SMS to greet customer on his birthday
Proposed Solution
We can make use of Twilio which is a third-party vendor to send an SMS from CRM dynamics.
Steps to achieve this requirement,
- Create custom Activity with name SMS and add fields(Subject - Text data type, From - Text data type, To - Text data type, SID - Text) on to it. SID field is used to store the unique ID from Twilio in response which can be used to track the status of outbound SMS.
- Get a Twilio 14 days trial account here
- Get Twilio Trial Number by follow step by step tutorial here
- Add Twilio DLL Install-Package Twilio? and use ILMerge to merge this DLL with plugin DLL
- Register Post Create plugin on custom activity SMS and use below code to send SMS from CRM.
// Find your Account Sid and Token at twilio.com/console
// DANGER! This is insecure. See http://twil.io/secure
const string accountSid = "";// Find your Account Sid and Token at twilio.com/console
const string authToken = "";// Find your Account Sid and Token at twilio.com/console
TwilioClient.Init(accountSid, authToken);
var message = MessageResource.Create(
body: "Congratulations! You have selected for a trip to China. Please send you account details to [email protected]. We will credit your account with 10k USD.",
from: new Twilio.Types.PhoneNumber("+12564484903"),
to: new Twilio.Types.PhoneNumber("+917702943098") );
Console.WriteLine(message.Sid);//Store this in SID field which we have created on custom activity SMS.
Console.WriteLine(message.Status);//Use this to set OOB status field on custom activity SMS.
Notes
- Configure Account Sid and Token in configuration custom entity in CRM and use these configurations in plugin code.
- Configure from number in configuration custom entity in CRM and use these configurations in plugin code.
- Use steps mentioned here to update SMS status from Twilio to CRM dynamics.
- I have hardcoded the Account Sid, Token, body, to and from in my code sample. You will have to use plugin context to get subject and To number and use configurations entity record to get other details.
Conclusion
In today's blog, we learned how we can easily send SMS on various events from Dynamics CRM.
Hope this helps!