We've been able to integrate our postcode lookup service into Microsoft Dynamics CRM for some time, but our previous Adding Postcode Lookup to Microsoft Dynamics CRM example requires you to be able to customise the CRM server. If you're using an online version of Microsoft Dynamics CRM hosted by Microsoft or a Microsoft Partner such as Outsourcery, you can't make these kind of alterations. The solution? A few lines of Javascript that you can apply in the entity customisation screens will get you up and running without even having to contact your hosting provider, and best of all, no extra costs!
Register for a free trial
The first step is to register with us to set up a free trial of our postcode lookup service. This will only take a minute and will give you 50 free lookups per day to get you going.
Get your Ajax key
Generate an Ajax API Key and enable it for use on your CRM system by entering the domain name of your CRM server. For example, if you access your CRM system at https://example.hosting.com/MyOrganisation/, enter example.hosting.com in the "Add Domain Name" box and click "Add Domain". Make a note of your API key from the top of this page.
Customise the entity form's OnLoad event
In your Microsoft Dynamics CRM system, go to Settings > Customization > Customize Entities, and select the entity type you want to add postcode lookup functionality for, e.g. "Account". Under "Forms and Views", double-click on the "Form" item. Ensure the "OnLoad" event is selected and click "Edit".
Tick the "Event is enabled" option, then copy and paste the code shown below into the OnLoad function:
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://webservices.data-8.co.uk/javascript/loader.ashx?key=YOUR-AJAX-KEY&load=AddressCapture';
document.getElementsByTagName('head')[0].appendChild(script);
Replace "YOUR-AJAX-KEY" with the Ajax API Key you generated earlier, then click "OK" twice to return to the form designer.
Customise the postcode field's OnChange event
Select the "Address 1: Zip/Postal Code" field and click "Change Properties". On the "Events" tab, ensure the "onChange" event is selected and click "Edit".
Tick the "Event is enabled" option, then copy and paste the code shown below into the OnChange function. Don't worry if it seems like a lot of code, you don't have to go through and understand it all.
var license = 'FreeTrial';
var lines = 5;
var fixTownCounty = true;
var postcode = crmForm.address1_postalcode.value;
var options = [
new data8.option('MaxLines', lines),
new data8.option('FixTownCounty', fixTownCounty),
new data8.option('Formatter', 'NoOrganisationFormatter')
];
var postcodeLookup = new data8.addresscapture();
onPostcodeLookupAddressSelected = function(address) {
crmForm.name.value = address.RawAddress.Organisation;
crmForm.name.returnValue = address.RawAddress.Organisation;
crmForm.address1_line1.value = address.Address.Lines[0];
crmForm.address1_line1.returnValue = address.Address.Lines[0];
crmForm.address1_line2.value = address.Address.Lines[1];
crmForm.address1_line2.returnValue = address.Address.Lines[1];
crmForm.address1_line3.value = address.Address.Lines[2];
crmForm.address1_line3.returnValue = address.Address.Lines[2];
crmForm.address1_city.value = address.Address.Lines[3];
crmForm.address1_city.returnValue = address.Address.Lines[3];
crmForm.address1_stateorprovince.value = address.Address.Lines[4];
crmForm.address1_stateorprovince.returnValue = address.Address.Lines[4];
crmForm.address1_postalcode.value = address.Address.Lines[5];
crmForm.address1_postalcode.returnValue = address.Address.Lines[5];
};
function toProperCase(s) {
return s.toLowerCase().replace(/^(.)|\s(.)/g, function($1) { return $1.toUpperCase(); });
}
onPostcodeLookupComplete = function(results) {
if (!results.Status.Success) {
alert(results.Status.ErrorMessage);
}
else if (results.Results.length == 0) {
alert('Unknown postcode');
}
else if (results.Results.length == 1) {
onPostcodeLookupAddressSelected(results.Results[0]);
}
else {
postcodeLookupWindow = window.open('', 'Data8PostcodeLookup', 'height=300,width=400,toolbar=no,directories=no,status=no,menubar=no,scrollbacks=no,resizable=no,modal=yes,left=' + (window.screenLeft + (document.body.clientWidth - 400) / 2) + ',top=' + (window.screenTop + (document.body.clientHeight - 300) / 2));
postcodeLookupWindow.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
postcodeLookupWindow.document.write('<html>');
postcodeLookupWindow.document.write('<head>');
postcodeLookupWindow.document.write('<style type="text/css">');
postcodeLookupWindow.document.write('body { background-color: #D6E8FF; font-family: tahoma, verdana, arial; font-size: 11px; padding: 15px; }');
postcodeLookupWindow.document.write('#addresslist { border: solid 1px #69c; position: absolute; top: 0; bottom: 0; left: 0; right: 0; background-color: white; overflow-y: scroll; margin: 15px; padding: 2px; }');
postcodeLookupWindow.document.write('#addresslist div { cursor: pointer; white-space: nowrap; }');
postcodeLookupWindow.document.write('#addresslist div:hover { color: blue; text-decoration: underline; }');
postcodeLookupWindow.document.write('</style>');
postcodeLookupWindow.document.write('</head>');
postcodeLookupWindow.document.write('<body>');
postcodeLookupWindow.document.write('<div id="addresslist"></div>');
postcodeLookupWindow.document.write('</body>');
postcodeLookupWindow.document.write('</html>');
for (var i = 0; i < results.Results.length; i++) {
results.Results[i].RawAddress.Organisation = toProperCase(results.Results[i].RawAddress.Organisation);
var singleLine = results.Results[i].RawAddress.Organisation;
for (var line = 0; line < results.Results[i].Address.Lines.length; line++) {
if (results.Results[i].Address.Lines[line] != '') {
if (singleLine != '')
singleLine += ', ';
singleLine += results.Results[i].Address.Lines[line];
}
}
var div = postcodeLookupWindow.document.createElement('div');
div.innerText = singleLine;
var configureClick = function(address) {
div.onclick = function() {
onPostcodeLookupAddressSelected(address);
postcodeLookupWindow.close();
};
};
configureClick(results.Results[i]);
postcodeLookupWindow.document.getElementById('addresslist').appendChild(div);
}
postcodeLookupWindow.focus();
}
};
postcodeLookup.getfulladdress(license, postcode, '', options, onPostcodeLookupComplete);
Click "OK" twice to return to the form designer, then "Save and Close" to save the changes to the form. Under the "Actions" menu, click "Publish" to make the changes live.
Done!
That's it, you've now got a full postcode lookup solution for your CRM accounts entry form. Just enter the postcode into the postcode field and press tab - if there's only one address at that postcode the address will automatically be filled in, otherwise you'll be shown a list of addresses to choose from.
Once you have checked that everything is working correctly and have purchased the full service, you will need to change the first line of the script to "var license = 'InternalUserFull';" and re-publish the changes.
Customising for Leads or different Account fields
The script above is written to work with the default Account fields. If you've customised the address fields, or want to use postcode lookup for a different entity type such as leads, you will need to modify the script slightly. There are several ways it can be modified, e.g.
- Changing the names of the fields that the address details are stored in
- Ignoring the company name details, or including them in the address fields
- Changing the number of fields the address is split over
- Allowing the town and county information to move according to the number of other lines in a particular address
If you would like any help customising this script for cases like these, please contact us and we'll be happy to help.