Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 82002

Select correct value from Autocomplete List

$
0
0

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?

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);
        }
    }
}

Viewing all articles
Browse latest Browse all 82002

Latest Images

Trending Articles



Latest Images