Reassign leads on zip codes
Last week I received a very interesting requirement during one of my discovery calls, which Iām sure many of you have heard before or maybe even wanted to implement yourself! The sales manager wanted to assign leads based on the zip code of the lead. They explained to me that each zip code would be assigned to one rep and that sales reps have multiple zip codes assigned to them. Each time a lead comes in, they would want Dynamics 365 to automatically assign the lead to the rep that owns that zip code. I thought this was a very interesting way of assigning leads and started thinking about how we can accomplish this with the tools that are available to us today using the Power Platform. Keep in mind, not too long ago this wasnāt something we could easily do; we would have to get a developer involved to create a plugin to get this accomplished!
Zip Code Entity
The first thing I did was create a new entity called āZip Codesā. I made this an organization owned entity, but of course you can also choose to make this user or team ownership and then use the out of the box owner field to store the sales rep (user record) in that field. I created a system user lookup field called āSales Repā and added it to the form. This is the field I am using to show which rep is assigned to the zip code record. Then I updated the āActive Zip Codesā view by adding the āSales Repā field to the view. Done! That was easy enough!
Now of course I could remove the out of the box zip code field āaddress1_postalcodeā (which is a single line of text field) from the lead form and replace it with a lookup to my zip code entity, but I wanted to keep the out of the box zip code field on the form. (I.E Other add-ons like Inside View or D&B populate data in the out of the box zip code field which is why I wanted that field to stay on the form.)
Configure out of the box zip code field control
I configured the address1_postalcode field using the auto-complete control on the form. This control allows admins to āpointā a single line of text field to values in either an entity related option set type of field or values in an entity view. (What this means is that the single line of text field kinda turns into an option set type of field.)
When you configure the control to use a view you have to define the entity you want to use and the entity view. Obviously I choose to use a view and I selected the āZip Codesā entity and the āActive Zip Codesā view. Lastly I needed to select the field of the viewās primary entity to use as the data source. This will normally be the primary entity field, which I had renamed from ānameā to āzip codeā when I created the entity.
I uploaded some zip code records in the system and tested my form. It looked good and worked exactly how I intended it to work!

Assign lead to sales rep
Now lets focus on the automation of this. What we need the system to do is take a look at the zip code of the lead, and then look at our zip code entity and find the zip code record that matches the zip code on the lead record. We all know we canāt query the data base using work flows, so I am going to use Power Automate for this.
I logged into Power Automate and clicked the ā+ Newā button to create a new flow. I selected āautomated from blankā and then āwhen a record is createdā under the common data service. When clicking āCreateā the flow page is loaded and you need to pick your environment and entity name. I selected ācurrent environmentā and selected the lead entity. For scope I selected āorganizationā. The next step is to initialize a variable, this is where I will store the value of the lead recordsā zip code from Dynamics 365. I named it D365 Zip, but obviously you can name it whatever you want. For Type I selected String and I selected āZip/Postal Codeā as the value. (This is the field on the lead form.) The next step is to initialize another variable also with a type of string. This is where we will store the value of the sales rep related to the zip code record. Right now by creating this step, weāre just creating an empty container where we can store this data in later.
The next step in this flow is ālist recordsā (common data service). In this step we are querying the Dynamics 365 data base. We will be using the zip code that was entered in the lead record as a filter. Since we are looking for zip code records, thatās what we enter as the entity. Under filter query, I typed the primary field of the entity (in my case ānfb_zipcodeā) then āeqā and then I picked the variable D365 Zip. Make sure you use single quotes around the variable! We are only expecting to get one record in the query, so set the top count to 1. Below is an example of what this looks like.

In the next step Iām going to store data from the zip code record that we got from the query, into the variable (container) we created earlier. I clicked ā+ next stepā and selected āAppend to string variableā. For the name, select āSales Rep from Zipā from the drop down. I clicked my mouse in the value field and from below the ālist recordsā I picked āSales Repā. This will add the āapply to eachā around the step, which is fine. If you would want to add additional field data, you can do this by clicking ā+ Add Actionā in the āApply to eachā box. Make sure you have enough string variables to add the values to.
Again I clicked on ā+ New Stepā and selected āUpdate a Recordā from the common data service. This is where I am going to reassign the lead to the sales rep related to the zip code record. I selected current environment, and āLeadsā as the entity. I want to update the lead that kicked off this flow, so after I clicked in the ārecord identifierā field under the āupdate a recordā step, I need to scroll down to the āWhen a record is createdā section to find the lead (unique identifier) field. I then expanded the advanced options and here is where I am going to reassign the lead. The first thing I did was set the āowner typeā field to āsystem usersā. I am not assigning it to a team. Then I click my mouse in the āOwnerā field and from under āvariablesā I select the āSales Rep from Zipā variable. Make sure to test your flow after you saved it!
I hope you enjoyed this post! Be sure to check back next week for a new article or subscribe here to never miss another post!
This was originally posted here.

Like
Report
*This post is locked for comments