bidou88
bidou88

Reputation: 694

Dynamics CRM 2016 : Autocomplete multiple fields

Here is the scenario :

When a user is entering a zip code, the autocomplete must be displayed, and when the user select a zip code, the others fields like the city and the county should be filled out automatically.

The information about zip code, city and zip code are in a JSON object.

Is there an event when the user clicked on the autocomplete list ?

Does anyone know how to achieve this ?

Thanks for your help

Upvotes: 1

Views: 1324

Answers (3)

T. Jensen
T. Jensen

Reputation: 21

I just tested your scenario using the code example that Polshgiant shared and added an onchange event on the attribute as suggested by Jorge Cunha.

Unfortunately my test shows, that only the keystrokes triggers the onchange event: When I select an autocomplete value, the onchange is not fired.

Thereby I see no supported solution for this question.

But I need the same functionality, so I hope others will prove me wrong.

Edit 17th May: A new test today show that the onchange event is called, but that

var newValue = Xrm.Page.getControl(control).getValue(); 

get the value typed, and not the autocompleted value.

However

var newValue = Xrm.Page.getControl(control).getAttribute().getValue();

gives me the autocomplete-selected value.

Thereby I can now use this functionality :-)

Upvotes: 2

Polshgiant
Polshgiant

Reputation: 3664

CRM 2016 recently added a keypress event that you can use for string fields that allow you to do exactly what you're trying to do (doesn't work on mobile, though). Here's the SDK's example:

/* Sample JavaScript code to demonstrate the auto-completion feature.
   This sample configures the auto-complete feature for the "Account Name"
   field in the account form. */

function suggestAccounts() {

    // List of sample account names to suggest
    accounts = [
  { name: 'A. Datum Corporation', code: 'A01' },
  { name: 'Adventure Works Cycles', code: 'A02' },
  { name: 'Alpine Ski House', code: 'A03' },
  { name: 'Bellows College', code: 'A04' },
  { name: 'Best For You Organics Company', code: 'A05' },
  { name: 'Blue Yonder Airlines', code: 'A06' },
  { name: 'City Power & Light', code: 'A07' },
  { name: 'Coho Vineyard', code: 'A08' },
  { name: 'Coho Winery', code: 'A09' },
  { name: 'Coho Vineyard & Winery', code: 'A10' },
  { name: 'Contoso, Ltd.', code: 'A11' },
  { name: 'Proseware, Inc.', code: 'A30' },
  { name: 'Relecloud', code: 'A31' },
  { name: 'School of Fine Art', code: 'A32' },
  { name: 'Southridge Video', code: 'A33' },
  { name: 'Tailspin Toys', code: 'A34' },
  { name: 'Trey Research', code: 'A35' },
  { name: 'The Phone Company', code: 'A36' },
  { name: 'VanArsdel, Ltd.', code: 'A37' },
  { name: 'Wide World Importers', code: 'A38' },
  { name: '​Wingtip Toys', code: 'A39' },
  { name: 'Woodgrove Bank', code: 'A40' }    
    ];

    var keyPressFcn = function (ext) {
        try {
            var userInput = Xrm.Page.getControl("name").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();
            }
        } catch (e) {
            // Handle any exceptions. In the sample code,
            // we are just displaying the exception, if any.
            console.log(e);
        }
    };

    Xrm.Page.getControl("name").addOnKeyPress(keyPressFcn);    
}

Then you can use the addOnChange event to process the user's selection.

Upvotes: 0

Jorge Cunha
Jorge Cunha

Reputation: 60

From the CRM of the box you don't have anything do what you are asking, but you have 3 possibilities to do that.

1 Create a Entity for your zip code and put your info inside. After create the lookup on your form the only way I remember to do on supported way.

2 On the event change of the field call your method js to auto populate but whit this the field need to be full writhed, for you script to get the matching.

3 this is not supported but you can manipulate the DOM and put you js script to auto populate your fields whit a event on the field.

Cheers

Upvotes: 0

Related Questions