Following on from part 1-receiving sms in dynamics using twilio. You have created a flow webhook which receives sms messages sent to your Twilio number and creates an activity in Dynamics.
This guide will focus on finding a match between the incoming mobile phone number with a contacts in Dynamics, and then associate the activity with the contact record.
The simplest way i have found of doing this is to query dynamics for contacts whose mobile phone number ends with the last 10 digits of the incoming number. Twilio returns numbers as for example +4407415066772 , if you attempt to match the entire string it`ll fail if the contacts mobile number value is 07415066772 or 00447415066772. Taking the last 10 digits 7415066773 and trying to find a number that ends with that value, solves both the above problems.
High level summary, once the sms is received in flow
- Get the last 10 digits of the incoming number
- Query dynamics for a contact whose mobile number field value ends with the last 10 digits
- If a match is found create an activity and set regarding to the contact
Add an initialize variable step under the Apply to each step with the following details.
- Name : FromContactGuid
- Type : String
- Value : null
Add a compose action, this is in Data Operations – Compose. Rename this to TrailingNumbers by selecting the 3 dot menu icon in the top right corner then rename. This is going to be used to trim the last 10 digit of the incoming number. Place your mouse cursor into the Input entry to open the dynamic content box ( if this doesn’t open, make sure your flow editor screen is in full screen, or zoom out). Switch to the Expression tab and copy and paste in the below function and hit okay.
TextFrom is the variable created in Part 1 contain the incoming number of the text, if you named this differently, replace in the formular. The above formula cuts the last 10 digits of the mobile number . The output of the TrailingNumbers compose step will now contain the last 10 digits of the mobile number.
Dynamics – List Records
Next step is to search Dynamics for a match, add a List records action located in Dynamics 365 -> List records. Select your Dynamics instance and the Contacts entity. Expand show advanced options to reveal the additional inputs. In the Filter Query add
This is an OData filter query which will search the mobilephone field in Dynamics for numbers that end with the output of the TrailingNumbers compose step.
Apply to Each – Set Variable
Add an Apply to each control. Add the Value of the List records step as its parameter, this will loop through the contact records returned. Add a Set variable action within. Select the FromContactGuid variable and set its value to the Contact guid using the dynamic content box or copy and paste in the below formula which should resolved to the contact id.
Condition – Create New Record
Add Condition step after the apply to each and set its parameters to FromContactGuid variable is not equal to null.
In the If Yes portion of the condition, if a matching contact has been found add a Dynamics create new record action. Connect to your dynamics instance , select your text messaging entity and add your variables to the corresponding fields in dynamics the TextFrom number and your text message body field. Also set your text message status field to the value of your received option . Finally set the regarding field to the FromContactGuid and the regarding type to contacts. Use the screenshot below as a reference although im using the phone call activity as an example
In the If No section of the condition you can handle creating an unknown sms activity in case a matching contact is not found, again using the phone call entity as an example.
The completed flow should look like the below