web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CustomControl Autocompelte: Can't set value in onchange

(0) ShareShare
ReportReport
Posted on by 145

hi,

I've added the customcontrol AutoComplete to 2 formfields (address1_postalcode, address1_city) and configured it with a custom entity where I have many addresses stored.

When I type in an postalcode a list of all matching postalcodes is show ---> thats fine.

But when I add an onchange event (to set the postalcode or the city - depending on the changed field) I get no error and It simply will not set the field-value.

this is my code:

function initAutocomplete(context, postalcodeFieldName, cityFieldName) {

    var formContext = context.getFormContext();

    var fields = [postalcodeFieldName, cityFieldName];

    for (var i = 0; i < fields.length; i++) {
        formContext.getAttribute(fields[i]).addOnChange(function (executionContext) {
            autocompleteCalled(executionContext.getFormContext(), countyFieldName, postalcodeFieldName, cityFieldName, executionContext.getEventSource());
        });
    }
}


function autocompleteCalled(formContext, postalcodeFieldName, cityFieldName, sourceField) {

    //"address1_postalcode", "address1_city", "address1_stateorprovince"
    var sourceName = sourceField.getName();
    var sourceValue = sourceField.getValue();
    if (sourceValue == null)
        return;

    var column = "";
    switch (sourceName) {
        case postalcodeFieldName:
            column = "bsw_postalcode";
            break;

        case cityFieldName:
            column = "bsw_city";
            break;
        default:
            return;
    }

    var filter = $bsw.string.format("{0} eq '{1}'", column, sourceValue);
    var first = $bsw.remote.retrieveFirst("bsw_geodata", "bsw_postalcode,bsw_city", filter);

    if (!first)
        return;

    console.log(postalcodeFieldName + "=" + first["bsw_postalcode"]);
    console.log(cityFieldName + "=" + first["bsw_city"]);

	formContext.getAttribute(countyFieldName).setValue(first["bsw_stateorprovince"]);
	formContext.getAttribute(postalcodeFieldName).setValue(first["bsw_postalcode"]);
}


i also tried to use Xrm.Page directly in the console of chrome to set any text without success..

any ideas?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    What version of Dynamics are you on? Version 9?

    Why are you using jquery to query the data from another entity. You should be using Xrm.WebApi.

    Hope this helps.

  • T.Koller Profile Picture
    145 on at

    hi Aric,

    I'm using Dynamics CRM Online with the latest updates.

    But this is not a problem of retrieving data from anywhere (jquery, Xrm.WebApi,...).

    Its a problem to set a value to the customControl "autocomplete" (or whatever its called).

    When I simply use

    formContext.getAttribute("address1_city").setValue("MyCity")


    it doesn't show the changes.

    When I programmatically get this value again by using

    formContext.getAttribute("address1_city").getValue()


    I can see that the changes were made...

    Best Regards

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans