MSCRM :: NZ Post Auto Complete

After reading John’s Blog about using the NZ Post Air and the new Auto Complete functionality in MSCRM I thought to myself how can i take this to the next level.
ref: https://www.magnetismsolutions.com/blog/johntowgood/2017/05/10/new-zealand-post-auto-complete-in-dynamics-365
// JavaScript source code // Updated 06/03/2018 // Developed By // Gareth Cheyne, Harvey Norman NZ // eMail gareth.cheyne@nz.harveynorman.com //- Build off original code from Magnetism. //- https://www.magnetismsolutions.com/blog/johntowgood/2017/05/10/new-zealand-post-auto-complete-in-dynamics-365 // Note for use on any Form with Address Input. // add HN.setUpAddressSuggestions to OnLoad Event, and add the schema name of line 1 address field. // ie shipto_line1, or address1_line1 default is address1_line1 // // add HN.onSaveAddress to OnSave Events, and add the sche scheed name of the line 1 address field. // ie shipto, or address1 default is address1 //Creds for NZ Post API var CLIENT_ID = "xxx-xxx-xxx" var CLIENT_SECRET = "xxx-xxx-xxx" var NZ_POST_URI = "https://api.nzpost.co.nz/privateaddresschecker/1.0/" if (typeof (HN) == typeof (undefined)) { HN = {}; HN.onChangeAddress = function () { console.log("onchange"); console.log(Xrm.Page.getAttribute("address1_line1").getValue()) } HN.onSaveAddress = function (address_set) { //set default Address Set if no value is passed. if (address_set == null || address_set == undefined) { var address_set = "address1"; } try { var address = Xrm.Page.getAttribute(address_set + "_line1").getValue() console.log(address); if (address != null) { var address_DPID = address.split("DPID:"); if (address_DPID[1].length > 1) { address_DPID = address_DPID[1]; HN.getDetailedAddress(address_set, address_DPID); } } } catch (e) { console.log(e) } } HN.setFullAddress = function (address_set, address_detailed) { Xrm.Page.getAttribute(address_set + "_line1").setValue(address_detailed[0].AddressLine1); Xrm.Page.getAttribute(address_set + "_line2").setValue(""); //Not userd, blanking field Xrm.Page.getAttribute(address_set + "_line3").setValue(address_detailed[0].Suburb); Xrm.Page.getAttribute(address_set + "_postalcode").setValue(address_detailed[0].Postcode); Xrm.Page.getAttribute(address_set + "_city").setValue(address_detailed[0].CityTown); Xrm.Page.getAttribute(address_set + "_country").setValue("New Zealand"); //Blanking Fields after NZ Post Lookup, not Used In New Zealand try { Xrm.Page.getAttribute(address_set + "_county").setValue(""); Xrm.Page.getAttribute(address_set + "_stateorprovince").setValue(""); } catch (e) { console.log(e) } } HN.getDetailedAddress = function (address_set, address_DPID) { //Create a query string var queryString = "details?type=All&dpid=" + address_DPID + "&max=20&client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET; //Encode Uri var encodedUriString = encodeURI(NZ_POST_URI + queryString); var req = new XMLHttpRequest(); req.open("GET", encodedUriString, true /* async */); req.setRequestHeader("accept", "application/json"); req.onreadystatechange = function () { if (this.readyState == 4 /* complete */) { req.onreadystatechange = null; if (this.status == 200 /* ok */) { var reqResults = JSON.parse(this.response).details; if (reqResults != null && reqResults.length > 0) { console.log(reqResults) } //Sent Results to set fields. HN.setFullAddress(address_set, reqResults) } else { console.log("Parse Error: "); } } }; req.send(); } HN.setUpAddressSuggestions = function (address_set) { //set default Address Set if no value is passed. if (address_set == null || address_set == undefined){ address_set = "address1_line1"; } try { Xrm.Page.getControl(address_set).addOnKeyPress(HN.populateSuggestions); } catch (e) { console.log(e) } } HN.populateSuggestions = function (ext) { try { var userInput = Xrm.Page.getControl(ext.getEventSource().getName()).getValue(); HN.getSuggestions(userInput, ext); } catch (e) { console.log(e); } } HN.getSuggestions = function (input, ext) { //Create a query string var queryString = "suggest?q=" + input + "&type=All&max=20&client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET; //Encode Uri var encodedUriString = encodeURI(NZ_POST_URI + queryString); var req = new XMLHttpRequest(); req.open("GET", encodedUriString, true /* async */); req.setRequestHeader("accept", "application/json"); req.onreadystatechange = function () { if (this.readyState == 4 /* complete */) { req.onreadystatechange = null; var suggestions = new Array(); if (this.status == 200 /* ok */) { var reqResults = JSON.parse(this.response).addresses; if (reqResults != null && reqResults.length > 0) { for (var i = 0; i < reqResults.length; i++) { // Build the values for the AutoComplete Array suggestions.push({id: reqResults[i].DPID, icon: "https://springtimesoft.co.nz/images/technologies/nzpost_wellington_auckland.png", fields: [reqResults[i].FullAddress + ". DPID: " + reqResults[i].DPID]}); } } //Pass suggestion array HN.setSuggestions(ext, suggestions) } else { var error = JSON.parse(this.response).error; console.log(error); HN.setSuggestions(ext, suggestions); } } }; req.send(); } HN.setSuggestions = function (ext, suggestions) { try { var resultSet = { results: suggestions, commands: { id: "sp_commands", icon:"https://springtimesoft.co.nz/images/technologies/nzpost_wellington_auckland.png", label: "NZ Post", action: function () { window.open("https://www.nzpost.co.nz/tools/address-postcode-finder"); } } }; if (resultSet.results.length > 0) { ext.getEventSource().showAutoComplete(resultSet); } else { ext.getEventSource().hideAutoComplete(); } } catch (e) { console.log(e); } } }
Recent Comments