Following on from Part 1 where we created a plugin to send text messages. In this version we will use Microsoft Flow instead of the plugin. Although flow is easier to set up, I personally prefer the plugin method, as it gives you a lot more flexibility as well as sending instantly, if being run synchronously. Flow can sometimes take a few minutes to start.
Firstly enable change tracking on your text message entity. This allows changes to the entity such as creates and updates to trigger flows.
Head to Microsoft Flow and create a new blank flow. Add a dynamics 365 trigger when a record is updated. Connect to your dynamics instance and select your text message entity.
Whenever a text message record is updated our flow will be lunched.In order to stop our flow sending multiple texts every time our activity is updated, we`ll add a condition based on the text message status field. Select add new step below our trigger and choose condition. Set the condition to your text message status is equal to the label value of your sending status.
Remember in part 1 a real-time workflow was created to change the status from draft to sending. The above condition will ensure the flow only run after the workflow has validated the contact mobile number and is ready.
In the yes section of he condition action, search for and add a new action – Twilio send text message . Give the connection a name and add your Twilio Account SID and access token found on your Twilio console and create your connection. Add the to phone number to your contacts mobile number field and text to the body field, in this case i am reusing the description field on the entity. The from phone number allows you to select numbers you have available in Twilio.
Add one final action under the send text message. Dynamics 365 update record. Update the current text message record and set its status to sent, this will prevent another text being sent when the activity is updated.
The next step after you now have the ability to send texts from dynamics is to work out how to receive texts back. This is actually easily doable. Twilio allows you to provide an endpoint, when a text is received to your number, Twilio will send a post request to your URL with the details of the text. You can use flow as this endpoint and configure it to create a text message record in dynamics with the details of the inbound text. I then also created a plugin which will for every inbound text match it to the correct contact if they exist in dynamics and set the regarding field appropriately. Hopefully I will post another guide on this soon!