Friday, 10 November 2017

How to use Auto Complete in Dynamic CRM.

 Auto Complete in Dynamic CRM.


Description:

In this example we explain that how to create Auto Complete TextBox in Dynamic CRM.or how to use Auto Complete in Dynamic CRM.or how to implement Auto Complete functionality in Dynamic CRM using XRM and JavaScript. Or Autocomplete in Dynamic 365 controls. Or Web API Autocomplete in Dynamic CRM.

Here we demonstrate that how to Auto Complete All the Accounts of the CRM in Lead Form Company Name so user can just type one character and easily select that already created Account through Auto Complete list.


To achieve Auto Complete just call the below function in Form Load of the Lead Entity in CRM.

Code :

  function GetAllAccount() {

        var accounts = [];
        //alert(AccountId);
        var serverUrl = Xrm.Page.context.getClientUrl();
        var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + "AccountSet";
        //alert(odataSelect);
        $.ajax(
        {
            type: "GET",
            async: false,
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: odataSelect,
            beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
            success: function (data, textStatus, XmlHttpRequest) {


                if (data.d.results != null && data.d.results.length > 0) {


                    for (var i = 0; i < data.d.results.length; i++) {
                        accounts.push({ AccountId: data.d.results[i].AccountId, Name: data.d.results[i].Name })

                    }
                }

            },
            error: function (XmlHttpRequest, textStatus, errorThrown) { }
        });


        var keyPressFcn = function (ext) {
            try {
                debugger;
                var userInput = Xrm.Page.getControl("companyname").getValue();
                resultSet = {
                    results: new Array(),
                    commands: {
                        id: "sp_commands",
                        label: "Learn More",
                        action: function () {
                            // Specify what you want to do when the user
                            // clicks the "Learn More" link at the bottom
                            // of the auto-completion list.
                            // For this sample, we are just opening a page
                            // that provides information on working with
                            // accounts in CRM.
                            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 < accounts.length; i++) {
                    if (userInputLowerCase === accounts[i].Name.substring(0, userInputLowerCase.length).toLowerCase()) {
                        resultSet.results.push({
                            id: i,
                            fields: [accounts[i].Name]
                        });
                    }
                    if (resultSet.results.length >= 10) break;
                }

                if (resultSet.results.length > 0) {
                    ext.getEventSource().showAutoComplete(resultSet);
                } else {
                    ext.getEventSource().hideAutoComplete();
                }
                Xrm.Page.data.entity.save();

            } catch (e) {
                // Handle any exceptions. In the sample code,
                // we are just displaying the exception, if any.
                console.log(e);
            }
        };

        Xrm.Page.getControl("companyname").addOnKeyPress(keyPressFcn);

    }


This entry was posted in :

0 comments:

Post a Comment