Skip to main content

Notifications

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

}

  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at
    RE: Get lookup who's name matches a text field on the same form.

    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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

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

#1
Siv Sagar Profile Picture

Siv Sagar 149 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 61 Most Valuable Professional

#3
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 53 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans