17. Oktober 2017 11:39
var xoOurCompanyNamex = xoOurCompanyNamex || {};                
xoOurCompanyNamex.CityAutoComplete = xoOurCompanyNamex.CityAutoComplete || {};  
xoOurCompanyNamex.CityAutoComplete.registerCityAutoComplete = function (fieldname) {   // Definition der register-Funktion
   if (Xrm.Page.context.client.getClient() == "Mobile") {                // Xrm = Clientseitiger Namespace des CRM
      // Auto-completion not supported in mobile clients
      return;
   }   
   var field = Xrm.Page.getControl(fieldname);                  // The parameter mentioned in the web ressource
   
   if (field != null) {
      // keyPress-Event registrieren -> Notification, wenn User tippt
      field.addOnKeyPress(xoOurCompanyNamex.CityAutoComplete.onCityKeyPress);             
      field.getAttribute().addOnChange(xoOurCompanyNamex.CityAutoComplete.cityOnChange);  
   }
   else {
      /
      Xrm.Utility.alertDialog("There is no field \"" +
        fieldname + "\" on the form.\n Please check autocomplete registration.");
   }
}
xoOurCompanyNamex.CityAutoComplete.onCityKeyPress = function (ext) {          
   
   var source = ext.getEventSource();      
   var attributeName = source.getName();   
   
   var userInput = source.getValue();     
   
   // only provide suggestions if user typed at least three characters
   if (userInput == null || userInput.length < 3) {
      source.hideAutoComplete();
      return;
   }
   
   
   
   var resultSet = {
      results: new Array(),
      commands: null
   };
   
   //
   // Just for the test:
   //
   resultSet.results.push({ id: "Köln", fields: ["Köln","Deutschland","NRW"] });   
   resultSet.results.push({ id: "Gelsenkirchen", fields: ["Gelsenkirchen","Deutschland","NRW"] });
   resultSet.results.push({ id: "Salt Lake City", fields: ["Salt Lake City","USA","Utah"] });
   resultSet.results.push({ id: "Hamburg", fields: ["Hamburg","Deutschland","Hamburg"] });
   resultSet.results.push({ id: "München", fields: ["München","Deutschland","Bayern"] });
   resultSet.results.push({ id: "Paris", fields: ["Paris","Frankreich",""] });
   
   source.showAutoComplete(resultSet);
}
xoOurCompanyNamex.CityAutoComplete.cityOnChange = function (ext) {
   var source = ext.getEventSource();
   var city = Xrm.Page.getAttribute(source.getName()).getValue();
   
   if (city != null || city.length > 0) {
            
      // Test:
      var country;
      var region;
      
      switch (city) {
         case "Köln":
            country = "Deutschland";
            region  = "NRW";
            break;
         case "Gelsenkirchen":
            break;
         case "Salt Lake City":
            country = "USA";
            region  = "Utah";
            break;
         case "Hamburg":
            country = "Deutschland";
            region  = "Hamburg";
            break;
         case "München":
            country = "Deutschland";
            region  = "Bayern";
            break;
         case "Paris":
            country = "Frankreich";
            region  = "";
            break;
         default:
            country = "Deutschland";
            region  = "";
         
      }
      
      
      Xrm.Page.getAttribute("address1_country").setValue(country);
      Xrm.Page.getAttribute("address1_stateorprovince").setValue(region);
      
      
      Xrm.Page.getAttribute("address1_country").setSubmitMode("always");
      Xrm.Page.getAttribute("address1_country").fireOnChange();
      
   }
}