[vc_row][vc_column][cq_vc_zoomimage displaystyle=”magnify” magnifyimage=”6845″ bordercolor=”#ffffff” glassx=”50″ glassy=”110″][vc_separator][vc_empty_space height=”50px”][vc_wp_text title=”Summary”]One of the most common requirements requested by Clients is the address lookup functionality. OTB (Out-the-box) Microsoft Dynamics doesn’t have any address lookup capabilities. There is some integration with Microsoft Bing, but this feature only displays a map and does not resolve to an actual address.
In this post, I’m going to demonstrate how easy it is to use Google Maps and Dynamics CRM to provide address Lookup functionality.
[/vc_wp_text][/vc_column][/vc_row][vc_column][/vc_column][vc_row][vc_column][vc_wp_text]Just before I begin I want to point out that Google manages interaction with their services through licences. As of June 2017, you can carry out 1000 postcode searches without the need to purchase a licence on the free subscription, your use case may differ if you intend to use this differently. My advice is; read through the documentation here: https://developers.google.com/maps/pricing-and-plans/ then select the license based on your use case, if you want to implement the Google address lookup feature into a production environment.[/vc_wp_text][vc_empty_space][vc_wp_text title=”What are we going to accomplish?”][/vc_wp_text][vc_wp_text][/vc_wp_text][vc_wp_text title=”How it works”]
- You enter a postcode in the CRM Text Field.
- A function called GetPostCodeValue() is triggered. This function extracts the postcode that you entered in CRM.
- Google Maps is then queried with the postcode extracted using Google Maps API.
- The address returned, is then automatically populated in Dynamics CRM.
[/vc_wp_text][/vc_column][/vc_row][vc_row][vc_column][vc_wp_text title=”Source Code”]We’re going to be using JavaScript to query the Google Maps API. Here is the code that takes the postcode details, sends it to Google Maps and returns the address, automatically populating the Dynamics 365 fields:[/vc_wp_text][vc_wp_text]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
//----------------------------------------------------------------------------// //Purpose: Google Maps Postcode Lookup Solution for Dynamics CRM //Author: Syed Hussain //Date: 3rd June 2017 //Website: syedhussain.net //Email: sh@syedhussain.net //----------------------------------------------------------------------------// function GetPostCodeValue() { var postcode = Xrm.Page.getAttribute("address1_postalcode").getValue(); if (postcode != null) { FindAddress(postcode); } } function FindAddress(postcode) { jQuery.post('https://maps.googleapis.com/maps/api/geocode/json?address=' + postcode + '&sensor=false', function(r) { var lat = r['results'][0]['geometry']['location']['lat']; var lng = r['results'][0]['geometry']['location']['lng']; $.post('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + lat + ',' + lng + '&sensor=false', function(address) { var address_number = address['results'][0]['address_components'][0]['long_name']; if (address_number != null) { Xrm.Page.getAttribute("address1_line1").setValue(address_number); } var address_street1 = address['results'][0]['address_components'][1]['long_name']; if (address_street1 != null) { Xrm.Page.getAttribute("address1_line2").setValue(address_street1); } var address_street2 = address['results'][0]['address_components'][2]['long_name']; if (address_street2 != null) { Xrm.Page.getAttribute("address1_line3").setValue(address_street2); } var city = address['results'][0]['address_components'][3]['long_name']; if (city != null) { Xrm.Page.getAttribute("address1_city").setValue(city); } //var city2 = address['results'][0]['address_components'][4]['long_name']; //var city3 = address['results'][0]['address_components'][5]['long_name']; var country = address['results'][0]['address_components'][6]['long_name']; if (country != null) { Xrm.Page.getAttribute("address1_country").setValue(country); } //var Postcode = address['results'][0]['address_components'][7]['long_name']; }); }); } //----------------------------------------------------------------------------// |
[/vc_wp_text][vc_wp_text]Note: I have commented out city2 & city3 because it’s not needed for what I’m trying to do. If you need to customise what is returned, your first starting point should be: https://maps.googleapis.com/maps/api/geocode/json?address=W1A%201AA[/vc_wp_text][/vc_column][/vc_row][vc_row][vc_column][vc_wp_text title=”Technical Points”]
- Requires JQuery, so make sure to include this with your solution.
- The data returned (address) is an object. You can query the object further to fine tune your results:
[/vc_wp_text][/vc_column][/vc_row][vc_row][vc_column][vc_wp_text title=”I just want the solution”]If you just want to install the solution file without writing the code to test this out for yourself; you can download the solution file here:
Google Maps Dynamics 365 Solution (284 downloads)
Once downloaded, import the solution into your test organisation and publish. You will have to add the two libraries: GMPostCodeLookup.js & JqueryMin.js. into the Contact form.
Once you’ve done that be sure to hook up the OnChange event to the postcode field:
[/vc_wp_text][/vc_column][/vc_row][vc_row][vc_column][vc_wp_text title=”Final Words”]
- The same code can be used for any entity; this includes the Account entity or a custom entity.
- Make sure to check the license type that you need with Google.
[/vc_wp_text][/vc_column][/vc_row][vc_row][vc_column][vc_raw_js]JTNDc2NyaXB0JTIwdHlwZSUzRCUyMnRleHQlMkZqYXZhc2NyaXB0JTIyJTNFJTIwJTBBZnVuY3Rpb24lMjBzZXR1cCUyOCUyOSU3QmlmJTI4Y29uc29sZS5sb2clMjglMjJmaXJlJTIyJTI5JTJDMSUyMSUzRG1zZyUyOSU3QnZhciUyMGIlM0QlN0JuJTNBJTIyJTIwSW50ZXJhY3Rpb24lMjIlMkNzJTNBJTIyRHluYW1pY3MlMjAzNjUlMjB3aXRoJTIwR29vZ2xlJTIwTWFwcyUyMGZvciUyMEFkZHJlc3MlMjBMb29rdXAlMjIlMkNiJTNBJTIyTmV3JTIyJTdEJTNCalF1ZXJ5LmFqYXglMjglN0J1cmwlM0ElMjJodHRwcyUzQSUyRiUyRnByb2QtMDQud2VzdGV1cm9wZS5sb2dpYy5henVyZS5jb20lM0E0NDMlMkZ3b3JrZmxvd3MlMkY5NDQwNTE1ZmI0YmY0OGYzYTFhOTU2MzA5OTE4Yzg3ZSUyRnRyaWdnZXJzJTJGbWFudWFsJTJGcGF0aHMlMkZpbnZva2UlM0ZhcGktdmVyc2lvbiUzRDIwMTYtMDYtMDElMjZzcCUzRCUyNTJGdHJpZ2dlcnMlMjUyRm1hbnVhbCUyNTJGcnVuJTI2c3YlM0QxLjAlMjZzaWclM0RpRU14Y0J6TXpBcE5nWWxGeWl6djNrdlR5Zm12bC1FU19RN0xvbDlHeHJVJTIyJTJDdHlwZSUzQSUyMlBPU1QlMjIlMkNkYXRhJTNBSlNPTi5zdHJpbmdpZnklMjhiJTI5JTJDY29udGVudFR5cGUlM0ElMjJhcHBsaWNhdGlvbiUyRmpzb24lMjIlMkNzdWNjZXNzJTNBZnVuY3Rpb24lMjhhJTJDYiUyQ2MlMjklN0IlN0QlN0QlMjklN0Rtc2clM0QxJTdEdmFyJTIwbXNnJTNEMCUzQmpRdWVyeSUyOHdpbmRvdyUyOS5zY3JvbGwlMjhmdW5jdGlvbiUyOGElMjklN0JqUXVlcnklMjh3aW5kb3clMjkuc2Nyb2xsVG9wJTI4JTI5JTNCc2V0dXAlMjglMjklN0QlMjklM0IlMEElMEElM0MlMkZzY3JpcHQlM0U=[/vc_raw_js][/vc_column][/vc_row]
Hi,
how would you do this for Composite fields ??
https://www.magnetismsolutions.com/blog/paulnieuwelaar/2013/10/22/working-with-dynamics-crm-2013-composite-fields-fly-out-menu
not really sure how to get this to work.
thank you,
Joseph
This will still work for composite fields. It shouldn’t make a difference.
Will it directly navigates into our google maps when we click on on the location. I tried in mobile app it was not navigating to the google map.
This doesn’t navigate to Google Maps. I might add this item in when I have time.
Do you have plans to update this for the v9 web api code changes related to Xrm.Page.GetAttribute?
Hi
thanks for the help
but how can i make a link to the bing maps instead og google maps ?
I added the managed solution. I don’t believe this is working anymore. Line 17 of javascript code is getting this message “Unable to get property ‘geometry’ of undefined or null reference”.