Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

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

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
    LeoAlt 16,329 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans