Dynamics CRM 365 Send SMS Using Twilio From Plugins

Business Requirement

We often get requirements from businesses to send SMS from Dynamics CRM to customer on certain actions.

  1. Send SMS when New Lead created to Customer.
  2. Send SMS when Status of Case is changed.
  3. 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,

  1. 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.
  2. Get a Twilio 14 days trial account here
  3.  Get Twilio Trial Number by follow step by step tutorial here
  4. Add Twilio DLL Install-Package Twilio? and use ILMerge to merge this DLL with plugin DLL
  5. 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

  1. Configure Account Sid and Token in configuration custom entity in CRM and use these configurations in plugin code.
  2. Configure from number in configuration custom entity in CRM and use these configurations in plugin code.
  3. Use steps mentioned here to update SMS status from Twilio to CRM dynamics.
  4. 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!