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)

Auto populate value of a field depending upon the lookup value of another field

(0) ShareShare
ReportReport
Posted on by

Hello everyone!

I need some help with javascript. I have a requirement such that if I select a value from one lookup say Member and if their is only one Client associated with that Member then the value in the Client lookup field must auto populate.  If there are more than one Client lookup associated with the Member then it must provide a list of all the look up values. Note that both the lookup fields are present on the same form. 

I have made some progress with the code, but my filtering is not happening as it should be. If anyone could help me with the same. 

 

function memberClient()

{

 

                var entityName, entityId, entityLabel, lookupFieldObject;

                lookupFieldObject = Xrm.Page.data.entity.attributes.get('new_member');

                if (lookupFieldObject.getValue() != null)

                {

                                 entityId = lookupFieldObject.getValue()[0].id;

                                entityId = entityId.replace('{', '').replace('}', '');

                                entityName = lookupFieldObject.getValue()[0].entityType;

                                entityLabel = lookupFieldObject.getValue()[0].name;

                                alert(entityId);

                                alert(entityName);

                                alert(entityLabel);

                }

                var req = new XMLHttpRequest();

                req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/new_memberclientassociations?$select=_new_client_value&$filter=_new_member_value eq " + entityId, true);

                req.setRequestHeader("OData-MaxVersion", "4.0");

                req.setRequestHeader("OData-Version", "4.0");

                req.setRequestHeader("Accept", "application/json");

                req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

                req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");

                req.onreadystatechange = function () {

                   if (this.readyState === 4) {

                   req.onreadystatechange = null;

                  if (this.status === 200) {

                 var results = JSON.parse(this.response);

                  for (var i = 0; i < results.value.length; i++) {

                                var _new_client_value = results.value[i]["_new_client_value"];

                var _new_client_value_formatted = results.value[i]["_new_client_value@OData.Community.Display.V1.FormattedValue"];

                        alert(_new_client_value_formatted);

                 alert(_new_client_value);

              var lookup = new Array();

               lookup[0] = new Object();

             lookup[0].id = _new_client_value;

             lookup[0].name = _new_client_value_formatted;

             lookup[0].entityType = new_client;

              Xrm.Page.getAttribute("new_client").setValue(lookup);

            }

        }

        else {

            alert(this.statusText);

        }

    }

};

req.send();

}

 

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi,

    You want to add javascript to the change event of the country and the load of your form (in case there's a country already selected).

    When the country changes, query for all the states using the OData endpoint (msdn.microsoft.com/.../gg985387.aspx). If there's just the one, assign it on the state and disable it. If there's more than one, enable it.

    To limit to only states for the country, I think you can do that from the customizations, but if not, you can definitely do it in JS by adding a presearch function to the state lookup like this: www.concurrency.com/.../filter-dynamics-crm-lookup-values-with-javascript

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

     Aiden

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Darshani,

    It seems that you want to filter the lookup field value based on another lookup field.

    You can do this in customization of the lookup field properties -"Related Records Filtering"

    For javascript you can follow this link:-

    crmpankaj.blogspot.in/.../crm-2015-javascript-lookup-filtering.html

  • Suggested answer
    Hasibur Rahman Profile Picture
    on at

    I wrote a article recently with option set  - you could use the same concept for lookup

    hrbtechtalk.com/.../crm-sample-javascript-to-change-the-values-of-option-set-dynamically

  • Verified answer
    Aiden Kaskela Profile Picture
    19,696 on at

    It's not just a filter on the lookup, there's also a component to fill the value if there's only one result in the lookup. That would need to be handled through javascript.

  • Community Member Profile Picture
    on at

    Hi Aiden, you seem to have understood my requirement very well.

    As mentioned by you in the above post I have used filter lookup and have been able to get the related values in the State field depending upon the country selected. If you remember, both state and country are lookup fields. But I am still not able to autopopulate the state if there is just one state associated with the country.

    The Odata link you sent me is too complex to undertand as I am new in this field.

    It would be very grateful of you if you could provide me with a javascript to achieve the said requirement.

    Thanks!

  • Community Member Profile Picture
    on at

    Hi Darshani,

    Probably if you use two optonset  will be able to provide the js code to do this

    Regards

  • Community Member Profile Picture
    on at

    Thanks for the reply Nithin, but the requirement is that of lookups and not optionset

  • Community Member Profile Picture
    on at

    Hi Darshani,

    try with this code it might help

    function preFilterLookup()

    {

    Xrm.Page.getControl("new_State").addPreSearch(function () {

    var stateObject = Xrm.Page.getAttribute("new_State").getValue();

    // Get the lookup Object, and extract the Value (ID and Text)

    if(stateObject != null)

    {

    var stateTextValue = stateObject[0].name;

    var stateID = stateObject[0].id;

    // Filter the cities by State

    fetchXml = "";

    // Apply the filter to the field

    Xrm.Page.getControl("new_City").addCustomFilter(fetchXml);

    }

    });

    }

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

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans