Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

From Javascript Get a Count of the number of Items in a lookup field

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Using Xrm JavaScript I would like to get a count of the number of items in a lookup.

I have two lookup fields A and B. A is used to filter B. Once B is filtered if it has only one item I would like to set it as the default selected item. The last part I have however I don't see documentation to get a count of the items in a lookup field

Categories:
  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: From Javascript Get a Count of the number of Items in a lookup field

    Thank you!

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: From Javascript Get a Count of the number of Items in a lookup field

    Sorry , there is no such method to count the lookup record.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: From Javascript Get a Count of the number of Items in a lookup field

    So far the Lookup Field A is filtering Lookup field B. That works out of the box, I don't have to do anything for that functionality.

    Are you saying that I can't retrieve the values or get a count from a lookup field object in Javascript?

    I think I can get the selected value and the object with the following:

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

       lookupControl = Xrm.Page.getControl("B");

    I'm guess neither of these allow you to get the count?

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: From Javascript Get a Count of the number of Items in a lookup field

    Hi , 

    You can refer below sample , on change of account lookup set the contact lookup if contact has only one record in the lookup.

            function OnchangeAccountLookupSetContact()
            {
    
                var req = new XMLHttpRequest();
                req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts?$select=_accountid_value,contactid,firstname,fullname,lastname&$filter=_accountid_value eq 644e1dd7-2a7f-18fb-b8ed-ed78c3f92c2b", true); // Pass account id from parent lookup
                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=\"*\"");
                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 _accountid_value = results.value[i]["_accountid_value"];
                                var _accountid_value_formatted = results.value[i]["_accountid_value@OData.Community.Display.V1.FormattedValue"];
                                var _accountid_value_lookuplogicalname = results.value[i]["_accountid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                                var contactid = results.value[i]["contactid"];
                                var firstname = results.value[i]["firstname"];
                                var fullname = results.value[i]["fullname"];
                                var lastname = results.value[i]["lastname"];
    
                                if (results.value.length == 1) {
                                    // Set Contact contact Lookup value
                                    var lookupValue = new Array();
                                    lookupValue[0] = new Object();
                                    lookupValue[0].id = contactid; // GUID of the lookup id
                                    lookupValue[0].name = fullname; // Name of the lookup
                                    lookupValue[0].entityType = "contact"; //Entity Type of the lookup entity
                                    Xrm.Page.getAttribute("FieldName").setValue(lookupValue); // You need to replace the lookup field Name..
                                }
                            }
                        } else {
                            Xrm.Utility.alertDialog(this.statusText);
                        }
                    }
                };
                req.send();
    
            }


  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: From Javascript Get a Count of the number of Items in a lookup field

    Hi,

    You need to write custom JavaScript for that , OOB dynamics CRM does not support this functionality.For your requirement you need to write one JS method on change of first lookup and retrieve the second lookup data by using CRM Rest API . Check if the records count is greater than 0 or not. Set the value accordingly.

    Download CRMRestBuilder - github.com/.../CRMRESTBuilder

    Set lookup value  -

    goutamdascrm.wordpress.com/.../how-to-set-lookup-field-value-using-javascript-dynamics-crm

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans