Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Select correct value from Autocomplete List

Posted on by 550

Hi together,

a wrote a script calling the autocomplete function for the different forms (contact, account, lead). This is a list with postalcodes (plz), cities and provinces (Bundesland in German).

The function "companyname_postalcodelistfunc()" is placed in the onload-trigger, the function "companyname_setvaluesofpostcodelist()" in the onchange trigger of the postalcode field. (There's a check function on the onsave as well, but this doesn't matter right now, because there are no problems)

It works fine. But... if there's a postalcode (plz) that stands for more than one city the last city is set in the city field (so to say the last of the shown array).

(Of course the list is much longer, but I shortened it for this posting)

So e.g. when I chose "plz: '53539', name: 'Borler', bundesland: 'Rheinland-Pfalz'", the value which is set is "plz: '53539', name: 'Welcherath', bundesland: 'Rheinland-Pfalz'"

What do I have to do to get the chosen value to be set in the fields?

Best regards
Mick

3173.2.PNG

774327.3.PNG

var companyname_postalcodeslist = [ 
				     { plz: '01454', name: 'Wachau', bundesland: 'Sachsen' },
                                     { plz: '01458', name: 'Ottendorf-Okrilla', bundesland: 'Sachsen' },
                                     { plz: '01465', name: 'Dresden', bundesland: 'Sachsen' },
                                     { plz: '53534', name: 'Wirft', bundesland: 'Rheinland-Pfalz' },
                                     { plz: '53539', name: 'Bodenbach', bundesland: 'Rheinland-Pfalz' },
                                     { plz: '53539', name: 'Bongard', bundesland: 'Rheinland-Pfalz' },
                                     { plz: '53539', name: 'Borler', bundesland: 'Rheinland-Pfalz' },
                                     { plz: '53539', name: 'Brücktal', bundesland: 'Rheinland-Pfalz' },
                                     { plz: '53539', name: 'Gelenberg', bundesland: 'Rheinland-Pfalz' },
                                     { plz: '53539', name: 'Kelberg', bundesland: 'Rheinland-Pfalz' },
                                     { plz: '53539', name: 'Kirsbach', bundesland: 'Rheinland-Pfalz' },
                                     { plz: '53539', name: 'Reimerath', bundesland: 'Rheinland-Pfalz' },
                                     { plz: '53539', name: 'Welcherath', bundesland: 'Rheinland-Pfalz' }
];


function companyname_postalcodelistfunc() {

    var resultPLZ = function (ext) {
        try {
            
            var userInput = Xrm.Page.getControl("address1_composite_compositionLinkControl_address1_postalcode").getValue();
            resultSet = {
                results: new Array()
            };

            var userInputLowerCase = userInput;
            for (i = 0; i < companyname_postalcodeslist.length; i++) {
                if (userInputLowerCase === companyname_postalcodeslist[i].plz.substring(0, userInputLowerCase.length)) {
                    resultSet.results.push({
                        id: i,
                        fields: [companyname_postalcodeslist[i].plz, companyname_postalcodeslist[i].ide, companyname_postalcodeslist[i].name],
                        name: companyname_postalcodeslist[i].name
                    });
                } // alert("1" + companyname_postalcodeslist[i].ide);
                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("address1_composite_compositionLinkControl_address1_postalcode").addOnKeyPress(resultPLZ);
    Xrm.Page.getControl("jobtitle").addOnKeyPress(resultPLZ);
}



function companyname_setvaluesofpostcodelist() {

    var companyname_postalCodevalue = Xrm.Page.getAttribute("address1_postalcode").getValue();
    
    
    for (var i = 0; i < companyname_postalcodeslist.length; i++) {
        if (companyname_postalCodevalue === companyname_postalcodeslist[i].plz) {
            
            var germany = "Deutschland";
            var cityfieldvalue = companyname_postalcodeslist[i].name;
            var provincefieldvalue = companyname_postalcodeslist[i].bundesland;
            Xrm.Page.getAttribute("address1_city").setValue(cityfieldvalue);
            Xrm.Page.getAttribute("address1_stateorprovince").setValue(provincefieldvalue);
            Xrm.Page.getAttribute("address1_country").setValue(germany);
        }
    }
}

*This post is locked for comments

  • Mick_4711 Profile Picture
    Mick_4711 550 on at
    RE: Select correct value from Autocomplete List

    Hi Ravi,

    it works fine now. Thank you!

    Best regards

    Mick

  • Mick_4711 Profile Picture
    Mick_4711 550 on at
    RE: Select correct value from Autocomplete List

    Hi Ravi,

    That's a nice idea.

    I'm gonna try it after my holidays and give a feedback directly.

    Best regards and many thanks

    Mick

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Select correct value from Autocomplete List

    Hi Mick,

    If I am not wrong, the only option is to concatenate the id to your plz i.e. plz + "[" + id + "]" and set this in the resultset such that in the results it will appears as 553539 [1], 553539 [2], 553539 [3]...

    And then when you get the selected values in method companyname_setvaluesofpostcodelist, instead of searching the plz, serch it based on the id.

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans