This week I was playing around with Power Virtual Agent and Power Automate. I needed to demo a virtual agent that after asking some questions would create a lead record in Dynamics 365 using Power Automate (formally known as flow). I haven’t worked with Power Automate that much, but after some internet searches, I was able to get it to do what I wanted so I thought I would share it with everyone.
Power Automate
The first thing I’m going to do is create the automation piece of this. I’m going to create a flow to create a new lead record in Dynamics 365, so I can call this flow from my virtual agent. First login to flow, then click on solutions. Make sure you select the environment where the virtual agent lives in the top right corner. Create a new solution. Click on the solution you just create to open it, then click +New on the top and select Flow.
In this example I’m going to capture the email address, first name and last name from the agent and then use those fields to create a new lead. In the search pane, type request and select the ‘When a HTTP request is received’ from the triggers. Now click on Use Sample Payload to generate the schema.
Type in {“emailaddress”:”email”,”firstname”:”first”,”lastname”:”last”} then click ‘Done’. (These will be the placeholders for the fields) The JSON should have loaded.
We then want to query the data base to make sure we don’t have a lead with this email address in the system already, we don’t want to create a duplicate lead.
To do this click +New Step and select ‘List Records – Common Data Service’. Select the correct environment and select the entity name, leads. In the filter query section type in ’emailaddress1 eq ‘drag the email address from the previous request step here’ and don’t forget the single quotes, circled in red on the screenshot below.
In the next step we’re going to see if the query came back with any records, if it didn’t that means we don’t have a lead in the system with this email address, which means we can create a new lead. If the query did come back with values, that means a lead is already in the system so we don’t want to create a duplicate. Click ‘+New Step’ and search for ‘condition’, then select the condition action from the list.
Click in the first field then click on ‘Expression’ in the dynamic content list. Add the following expression: empty(body(‘List_records’)?[‘value’]) (this is the outcome of the search). Set the next field to ‘is equal to’ click in the last field, and again click on ‘Expression’, then type ‘True’ and click ‘Ok’. (This means if the list came back empty)
Under the ‘If no’ section click add an action and select ‘Create a new record – Common Data Service’. Select the Dynamics 365 environment you want to create the record in and select the entity you want to create a record for (Leads). Click in the ‘Last Name’ field and select ‘lastname’ from “When a HTTP request is received’ in the dynamic content window on the right. Do the same for the ‘First Name’ and ‘Email Address’ fields. In the subject, type: ‘Bot Inquiry-‘ then add the first name and last name. It should look like the screenshot below. Feel free to enter data in any other fields if needed.
Now type the name of your flow in the top left of the page and click Save. That’s it! We configured our flow. Now lets go to the Power Virtual Agent and setup our topic! Login to Power Virtual Agent here, then click on Topics on the top left. Enter a title for your topic, and enter a few trigger phrases. (this will be phrases or words that will launch this topic when customer type this in) Click on ‘Go to authoring canvas’ on the right.
Power Virtual Agent Config
I love how easy we can configure these conversations and direct the conversation to different paths depending on answers from the customer. I’m going to keep this very simple.
In the ‘Bot Says’ window, type ‘Do you want us to contact you?’, then click on ‘User Says’. In the ‘User Says’ box, type ‘Yes’, click ‘User Says’ box again and type ‘No’. You will see that there is a branch that is automatically created, allowing us to go a different direction based on the answer.
We’re now going to capture the information needed to pass on to the flow we created earlier.
Under ‘User says no’ click ‘Bot Says’ and type in ‘First Name’, click ‘User Says’ and delete the user response by clicking on the trash can icon. Click on ‘Add Variable’ under ‘Output responses to’, then click create variable. Set ‘FirstName’ as the Variable name, leave the type as text, then click ‘done’. You see that a condition has been created automatically. Click on the ellipse of the condition box and delete it.
Click ‘Bot Says’ again and type in ‘Last Name’, click ‘User Says’ and delete the user response by clicking on the trash can icon. Click on ‘Add Variable’ under ‘Output responses to’, then click create variable. Set ‘LastName’ as the Variable name, leave the type as text, then click ‘done’. Click on the ellipse of the condition box and delete this one too. Click ‘Bot Says’ again and type in ‘Email Address’, click ‘User Says’ and delete the user response by clicking on the trash can icon. Click on ‘Add Variable’ under ‘Output responses to’, then click create variable. Set ‘ EmailAddress’ as the Variable name, leave the type as text, then click ‘done’. Click on the ellipse of the condition box and delete this one as well. Since for this example we want to keep it simple, I’m going to have the ‘Yes’ answer follow the same path as the ‘No’ answer just to keep it simple. Drag the connector under the ‘Yes’ condition to the first ‘Bot Says’ block under the ‘No’ condition.
In the next step we’re going to run our flow. Under the last response you added, click action, then select the flow you created in the previous steps. We now need to pass the information that was typed into the virtual agent to the flow. Under flow inputs, select the correct variables; emailaddress gets value from EmailAddress, etc.
Click End with Survey as the last step and save your bot.
Turn on tracing in the chat window on the left and click on ‘Start over with latest content’, then type in one of your triggers in the ‘Type your message’ box to start testing.
In order to see if your flow ran, you can navigate back to the solution and open the flow, the runs will be visible below the details window. I hope you enjoyed this post! Be sure to check in again soon for a new topic!