Create case from a tweet with negative sentiment
This past weekend I have been playing around a little more with Power Automate. I was off on Friday for Independence Day, since the 4th of July was on a Saturday most organizations gave their employees the previous Friday off to observe the holiday and what better to do on your day off than playing around with some of the tools of the Power Platform toolbox! I was thinking about various customer service scenarios and how there are so many platforms nowadays to reach out to customer service departments, which made me think about this topic. How cool would it be to create a case when a customer mentions a company on twitter with negative sentiment. Or we could create a hashtag customers can use, which would automatically create a case in Dynamics 365 if negative sentiment was detected in the Tweet. Of course we don’t want to create duplicate customer records, so I will also build in a check to make see this twitter user is in my Dynamics 365 database already. If they are I will related the case to the existing record, if they are not in my database I’ll create a new record to which I will relate the newly created case. I hope you like this idea as much as I did, if that is the case (pun intended) then keep on reading!
Create New Flow
I want to use the ‘current environment CDS’ connector in Power Automate so I need to create the flow from within the context of a solution. I can create a new solution or use an existing solution. For this purpose I am going to use a solution that I already have in my environment. Once I open the solution I can start building the flow by clicking the ‘+New’ button from the top right and selecting ‘Flow’. I usually start by typing in a name for the flow (before I forget) and I will call this flow ‘Negative Tweet to Case Flow’.
Select the twitter connector, and the ‘When a new tweet is posted’ trigger. In the search text is where we enter the search term we’re looking for, like a hashtag, a twitter handle or words like “bad customer service”. In my example I’m going to use #CustomerServiceDian. This means that when a new tweet is posted with this hashtag, my flow should pick it up and run the logic. The second step in this flow is to detect the sentiment of the tweet. NOTE: In order to use this step you need to have a text analytics connection. In the ‘Detect Sentiment’ action I will enter the dynamic content from twitter called ‘tweet text’. (This is the actual tweet that triggered the flow)
Split Twitter User Full Name
In the next step I am going to pick ‘Initialize Variable’ to get the twitter user name. Unfortunately the ‘name’ value from twitter is the full name of the twitter user, so I need to split that full name into a first name and a last name. In this ‘initialize variable’ step I set the name field to ‘twittername’, the type is a string and the value is the ‘name’ field from twitter. After this step I add another ‘initialize variable’ step, where the name is ‘first’, the type is ‘Array’ and the value is an expression that looks like this: split(variables(‘twittername’), ‘ ‘) The first name and last name in the twitter name are separated by a space, hence this expression. Now that we have split the first name and last name we can use these values later when we need to create a contact record.
Sentiment Score Condition & List Records
My next step (image above) will be a condition that will be looking at the sentiment score, the exact condition is ‘If the sentiment score is less than 1’. This condition will have a ‘If Yes’ branch and a “If No’ branch. The ‘If No’ branch isn’t going to go anywhere, and the flow will end there. If the score is indeed less than 1 (Yes branch), I want to perform a ‘list records’ step that will allow me to query Dynamics 365 for contact records to see if there is a contact in the database that has the same twitter user id as the person who submitted the tweet. If that person exists in my Dynamics 365 database, (we don’t want duplicates!) I want to create a case and attach the case to the pre-existing contact record. If that person does not exist in my database, I want to create a new contact, then create a new case and attach the case to the newly created contact. In order to track the twitter user id I added a field to the contact entity: ‘new_twitteruserid’. That is the field I will use to compare to the twitter user’s id. You also notice that I have the top count set to 1. This is because this query should only pull up 1 contact with that id. (If it would pull up more than one contact record that means there are multiple contacts with this twitter id, but this will prevent the system to pull up more than one record. If there are no contacts with that twitter user id, there will be 0 record in the list.
Get the number of records returned from list records step
The next step will be another condition, because I need to know if there is a contact record in the database or not, which we will be able to determine based on how many records were returned in the ‘list records’ step. If there was 1 record returned we know there is a contact record in the database representing the person who sent the tweet. If the value is not equal to 1, then we know there was no matching contact record found. In the condition I will enter the following expression: length(outputs(‘List_records’)?[‘body/value’]) is equal to 1. (this is where I am comparing the number of records returned in the list to 1) Under the ‘If Yes’ statement we will configure the logic to create a new case and relate it to the customer contact record.
Create Case for existing contact
I will add the ‘Create a new record’ step (current environment) where I will create a case and associate it with the contact we found in the ‘list records’ step. When adding this step Power Automate will automatically add a ‘Apply to each’ section, as shown above. Select ‘cases’ as the entity name. I will use the actual tweet as the case title and I will select ‘twitter’ in the origin field. Make sure when you relate the case to the contact record in the Customer(Contacts) field, to first type ‘/’ then the entity, in this case ‘contacts’, then an opening bracket, then enter the dynamic content from the list records step (contact) and a closing bracket as shown in the screenshot above.
If condition = no: Create contact record and case
Under the ‘If No’ statement we will first select the ‘create a new record’ – current environment to create a new contact record. Select ‘Contacts’ in the entity name drop down. In the ‘First Name’ field we will use the values from the array variable (called First) we created earlier. I’ll enter the following expression: variables(‘First’)[0] The 0 represents the first value we extracted (first name). In the ‘Last Name’ field I’ll enter the following expression: variables(‘First’)[1] The 1 represents the second value we extracted (last name). Screenshot below shows what this looks like in the flow.
Create case record and associate to contact
I’m going to create another step under the step I just finished, this is another ‘create a new record’ (current environment) which is where I’ll will create a new case. I’ll select ‘Cases’ as the entity name and enter the tweet text again as the case title. For the ‘Customer(Contacts) field, I’ll type ‘/’ then the entity, in this case ‘contacts’, then an opening bracket, then the dynamic content from the ‘create new record’ step (this is the previous step where we created a new contact record) (contact) and a closing bracket. Again we can set the origin fields to ‘twitter’ and populate any other fields if needed.
That’s it! Now you’re ready to test your flow! Good luck!
I hope you enjoyed this post! Be sure to check in again next week for a new topic or subscribe here to never miss another post!
Comments are Closed