Skip to main content

AutoComplete Feature for Dynamics CRM 2016

AutoComplete is one of the most desired and advanced feature for Dynamics CRM 2016. It not only enhances customer experience but helps in fast and accurate entry of records.
AutoComplete can be implementedon simple text fields as well as for complex and composite fields (e.g. Composite Address fields).
The example given below is for implementation of “Autocomplete feature” in Dynamics CRM 2016for simple text field such as Country in Address Details. One may simply type few characters in the field and a list of country names is displayed in sorted manner.
Assume the simple text field on which we want to enable autocomplete feature is “na_country” and an entity CountryMaster(na_countrymasters) contains the master list of countries.
Create a web resource say “AddressDetails.js” which will have 2 methods:

Method 1:LoadAutoComplete


This method is executed at the time of ‘Form Load’ to get the list of all countries existing in Country Master (na_countrymasters) entity. We are using CRM web API to get a list of countries sorted by name. (How to use web API is another topic and we will go into details later.)
function LoadAutoComplete() {
if (Xrm.Page.getControl(“na_country”) != null) {
Xrm.Page.getControl(“na_country”).setDisabled(true);
}
var clientURL = Xrm.Page.context.getClientUrl();
var oDataPath = clientURL + “/api/data/v8.0/”
var req = new XMLHttpRequest();
req.open(“GET”, encodeURI(oDataPath + “na_countrymasters?$select=na_name,na_countryname&$orderby=na_name”), true);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.onreadystatechange = function () {
if (this.readyState == 4) /* request complete*/ {
req.onreadystatechange = null;
if (this.status == 200) {
var reqResults = JSON.parse(this.response).value;
if (reqResults != null && reqResults.length > 0) {
var countries = [];
for (var i = 0; i < reqResults.length; i++) {
var country = reqResults[i];
countries.push({
id: country.na_countrymasterid,
name: country.na_name,
isocode: country.na_countryname,
fields: [country.na_name, country.na_countryname]
});
}
if (Xrm.Page.getControl(“na_country”) != null) {
Xrm.Page.getControl(“na_country”).setDisabled(false);
}
autoCompleteCountries(countries);
}
}
else {
var error = JSON.parse(this.response).error;
console.log(error.message);
}
}
};
req.send();
}
The above method calls the List of countries from Country Master entity and calls the second method AutoCompleteCountries().

Method 2: AutoCompleteCountries

This method works on “key press” event on Country field (The field on which autocomplete is applied)
function autoCompleteCountries(countries) {
var keyPressFcn = function (ext) {
try {
var userInput = Xrm.Page.getControl(“na_country”).getValue();
var resultSet = {
results: new Array(),
commands: {
id: “sp_commands”,
label: “Learn More”,
action: function () {
// Any help or other information URL
//window.open(“http://www.microsoft.com/en-us/dynamics/crm-customer-center/create-or-edit-an-account.aspx”);
}
}
};
var userInputLowerCase = userInput.toLowerCase();
for (i = 0; i < countries.length; i++) {
if (userInputLowerCase === countries[i].name.substring(0, userInputLowerCase.length).toLowerCase()) {
resultSet.results.push({
id: i,
fields: [countries[i].name, countries[i].isocode]
});
}
if (resultSet.results.length >= 10) break;
}
if (resultSet.results.length > 0) {
ext.getEventSource().showAutoComplete(resultSet);
}
else {
ext.getEventSource().hideAutoComplete();
}
} catch (e) {
console.log(e);
}
};
Xrm.Page.getControl(“na_country”).addOnKeyPress(keyPressFcn);
}

Setting in Dynamics CRM 2016:

Add the AddressDetails.js as new web resource in CRM.
Add this web resource to the form using form properties. Go to manage function and set LoadAutoComplete function on form Onload event

Publish the changes in CRM. Open the form and start entering letters in Country field. You should get the below autocomplete output
    please do get in touch with us at @
http://solzit.com/autocomplete-feature-fordynamics-crm-2016/@Solzit.com








Comments

Post a Comment

Popular posts from this blog

Dynamics CRM Development: Tips and Tricks – Calling a Workflow from Ribbon Button

There are a lot of quick ways of doing things in Dynamics CRM but developers struggle in finding how. Today I am going to explain how to call a workflow on a custom ribbon button. Let’s say you have a situation, where you want to perform some operation on customribbon button on “click event”. Let us take an example. I have a button on OPPORTUNITY form called‘ConfirmRequest ’. On the click event of this button I have to generate an ORDERandalso want to send an email. The best way to achieve this is by creating a workflow. Butthe issue is, how to call workflow on the button click. There are many solutions posted on the web, that involve using JavaScript, web resources etc. however there is a much easier way that requires no codin g. For this we use Ribbon Workbench. The same can be downloaded from the link  https://www.develop1.net/public/Download%20Ribbon%20Workbench%202013.aspx.  Download and install the solution in your CRM instance. Here are the steps to follow: Create

The “New” Business Process Flows in Dynamics 365

One of the reason that Dynamics scores over many other CRMs is the competency to create and customize business process flows and initiate workflows. The new Dynamics 365 announcement of CRM late last year has one of cool upgrading of the Business Process Flows. Business Process Flow in MicrosoftDynamics 365  is a tool, which is meant to help guide users through a business process in the system. Business process flows are representations of your business processes and are displayed visually in Dynamics 365 as a heading across the top of an entity form. This can be modified to suit the business process requirements for that entity. A business process flow is composed of Stages, and within each stage there are Steps to complete which are fields. In the business process flow heading, a user can see which stage they are at in the procedure, and which steps they want to complete before they proceed. Business process flows permit organizations to require users to complete cer

Harnessing Mobility with Dynamics 365 & Powerapps

Today, the business progressively depends on mobile technology. The next  generation  of enterprise mobility will be about  driving a continuous,integrated mobile experience – giving employees, customers, and partners the ability to pick up any device and have immediate access to all of the data, tools, interfaces, and controls they want. But they need to be able to do this without negotiating data security and integrity  Enterprise Mobile computing lets us tackle nearly any vocational  task  from  our  phones. Mobile  apps  for business  tasks  have  lagged  behind. Those  apps  that  do exist  have  not   always  been  as  comprehensive or available  as  desktop  solutions. Microsoft  saw  this issue and  presented  a cure: Microsoft Power Apps, which  is  part of   Microsoft Dynamics 365. Mobility with Dynamics 365 & Power Apps   Dynamics 365 is  the  cloud-bundled  version  of  its CRM  and  ERP  solutions. The  benefit  of  the  Power Apps  assimilat