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 365 | Integration, Dataverse...
Suggested Answer

Get lookup who's name matches a text field on the same form.

(0) ShareShare
ReportReport
Posted on by 270

I have a text field, I want to use some Javascript on it to find a related record with the same name and populate a lookup on the same form with the retrieved record.

Does anyone have a code example for this?

I was trying to use a web API request, but I believe i have some bad code around selecting a record where the name is 'eq' the same in the record being retrieved.

function retrieveAddressViaText() {

    var newid = Xrm.Page.getAttribute("new_zipcode").getValue();
    var req = new XMLHttpRequest();


    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl()   "/XRMServices/2011/OrganizationData.svc/new_postalcodeSet('new_PostalCode' eq '"   newid   "')?$select=new_State,new_City,new_Country,new_PostalCode", true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            this.onreadystatechange = null;
            if (this.status === 200) {

                alert(results.new_PostalCodeId);

                var result = JSON.parse(this.responseText).d;
                var State = result.new_State;
                var City = result.new_City;
                var Country = result.new_Country;
                var PostalCode = result.new_PostalCode;

                Xrm.Page.getAttribute("new_state").setValue(State);
                Xrm.Page.getAttribute("new_city").setValue(City);
                Xrm.Page.getAttribute("new_country").setValue(Country);
                Xrm.Page.getAttribute("new_zipcode").setValue(PostalCode);
                
                var lookup = new Object();
                var lookupValue = new Array();
                lookup.id = result.new_PostalCodeId.Id;
                lookup.entityType = "new_postalcode";
                lookup.name = result.new_PostalCodeId.Name;
                lookupValue[0] = lookup;
                Xrm.Page.getAttribute("new_zipcodelookup").setValue(lookupValue);
                Xrm.Page.getControl("new_zipcodelookup").clearNotification();

            } else {
                Xrm.Utility.alertDialog(this.statusText);
            }
        }

    };
    req.send();

}

I have the same question (0)
  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at

    Hi partner,

    If you are using D365 Online v9.X, I recommend you to use "Xrm.WebApi" for more easier developing.

    For example, you have a text field named field A in entity A form, and you want to find out a entity B record whose name equals the field A value and set the record to entity B lookup field on entity A form, please refer to the following code.

    function getValueFromLookUp(executionContext){
        var formContext=executionContext.getFormContext();
        //get fieldA 
        var fieldA=formContext.getAttribute("fieldA");
        if(fieldA!=null){
            //get fieldA value
            var fieldAValue=fieldA.getValue();
            //use record id to retrieve other field value
            Xrm.WebApi.retrieveMultipleRecords("entityB","?$select=new_name,new_entitybid&$filter=new_name eq" fieldAValue).then(
                function success(result){
                    for (var i = 0; i < result.entities.length; i  ) {
                        var lookupData = new Array();
                        var lookupItem = new Object();
                        lookupItem.id = result.entities[i].new_entitybid;
                        lookupItem.name = result.entities[i].new_name;
                        lookupItem.entityType = "entityB";
                        lookupData[0] = lookupItem;
                        formContext.data.entity.attributes.get("entityBid").setValue(lookupData);
                    }       
                },
                function (error){
                    console.log(error.message);
                }
            )
    
        }
        
    }

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/retrievemultiplerecords

    Best Regards,

    Leo

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 March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
11manish Profile Picture

11manish 149

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 79 Super User 2026 Season 1

#3
ManoVerse Profile Picture

ManoVerse 62 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans