How To Create Autocomplete on an Text Field in Dynamics CRM
Views (1190)
Scenario:
It might be requested you: when user types texts to a textbox, System helps to user with suggesting some values from couple entities in Dynamics CRM or maybe fetching records from any other resources.
Solution;
It is very simple, just register your code to the key press event for the object and fill the object with suggested values .
There is supported event for MSCRM engine (it has been so long since i do not call MSCRM :) ) calls : addOnKeyPress - when user presses a key over the object, the event is called for given delegated method
please consider performance and load on the network so;
1- do not fetch data from external resources each time user clicks the key - you can wait for the first 3 or 4 letters from the user
2- never fetch all matching records what user types so far, add "top 20 records" to your fetch method
Implementation:
first of all register the method while loading the form in Dynamics CRM:<Practice>
Create a javascript (.js) for each entity in Dynamics CRM for dedicated client scripting as much as possible.
</Practice>
<Practice>
method standards could be
form_onLoad(executionContext) {
var formContext = executionContext.getFormContext(); //to get the formContext
}
form_onSave(executionContext) {
var formContext = executionContext.getFormContext(); //to get the formContext
}
</Practice>now let's delegate our method to the event
function form_onLoad(executionContext) {
var formContext = executionContext.getFormContext(); //to get the formContext
formContext.getControl("<name of the textbox>").addOnKeyPress(<name of the textbox>_addOnKeyPress);
}
We should create a custom method : <name of the textbox>_addOnKeyPress which takes Execution Context parameterfunction <name of the textbox>_addOnKeyPress(executionContext) {
var formContext = executionContext.getFormContext(); //to get the formContext
var input = formContext.getControl("<name of the textbox>").getValue();
var resultSet = null;
if (input != null && input.length > 3) {
try {
// activate AutoComplete (or AutoFill)
resultSet = fetchDataMethodfromOtherSources(param1,param2,....);
}
} catch (exp) {
alert(exp);
}
}
if (resultSet != null && resultSet.results != null && resultSet.results.length > 0) {
executionContext.getEventSource().showAutoComplete(resultSet);
} else {
executionContext.getEventSource().hideAutoComplete();
}
} enjoy...
This was originally posted here.

Like
Report
*This post is locked for comments