SBX - Search With Button

SBX - Forum Post Title

Select correct value from Autocomplete List

Microsoft Dynamics CRM Forum

Mick_4711 asked a question on 7 Sep 2018 2:29 AM
My Badges

Question Status

Suggested Answer

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

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);
        }
    }
}
Reply
Ravi Kashyap responded on 16 Sep 2018 7:08 AM
Suggested Answer

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.

Reply
Mick_4711 responded on 16 Sep 2018 1:37 PM
My Badges

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

Reply
Ravi Kashyap responded on 16 Sep 2018 7:08 AM
Suggested Answer

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.

Reply

SBX - Two Col Forum

SBX - Migrated JS