You are currently browsing the tag archive for the ‘lookup’ tag.

Working with lookup fields in CRM when using JavaScript is a little different due to the way the entity is handled. The following snippets will allow you to both read a value in a look-up, as well as to populate a value in the look-up (for cases when you need to pre-define a value).

Reading the selected value in a lookup:

var lookupItem = new Array();
lookupItem = Xrm.Page.getAttribute("new_specialty").getValue();

if(lookupItem[0] != null)
{
    var name = lookupItem[0].name;
    var guid = lookupItem[0].id;
    var entType = lookupItem[0].entityType;
}

Writing to a lookup:

if(new_Country == null)
{
    // alert("Preparing to set Country:");
    var countryName = "Canada";
   
    var lookupData = new Array();
    var lookupItem = new Object();
    lookupItem.id="{A53E3645-912A-E111-92BB-00155D146C19}";
    lookupItem.name=countryName;
    lookupItem.entityType="new_country";
   
    lookupData[0] = lookupItem;
    // alert("Assigning Country to Field:");
    Xrm.Page.getAttribute("new_relatedcountryid").setValue(lookupData);
}

Enjoy!

Advertisements

In some instances, you might need to put a lookup on a form, and based on the selection in that lookup, retrieve additional data about the entity referenced. One such example could be either the standard entities like Account or Contact, or a custom entity you have created.

The following snippet will help you do that, but first, be aware of a few things:

1. Using OData, you can make calls back to the server, and get the values you need. Whether calling your custom entity, or the default ootb ones, the easiest way to determine what you are calling is to have a look at the XML returned by a call to the web service:

http://[servername]/[orgname]/XRMService/2011/OrganizationData.svc

the return XML will give you all the available entity names. Find the one you need in there, as you will be using it in the script.

2. Use the following script on lookup change event:

        try
        {
            //Get entity data;
            var serverUrl = Xrm.Page.context.getServerUrl();

            var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/new_specialtiesSet(guid’" + guid + "’)";
            // alert("ODATA Select: " + odataSelect.toString());
           
            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: odataSelect,
                beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
                success: function (data, textStatus, XmlHttpRequest) {
                    var org = data.d;
                    //Change form data
                    Xrm.Page.data.entity.attributes.get("new_specialtydescription").setValue(org.new_Description);
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    alert(‘OData Select Failed: ‘ + odataSelect);
                }
            }
            );
           
        }
        catch(err)
        {
            // handle the error

        }

3. Observe two highlighted sections:

new_specialtiesSet is the value obtained from the XML returned by the web service call. To this value we pass the GUID of the current entity, which we can get by using this script:

var lookupItem = new Array();
lookupItem = Xrm.Page.getAttribute("new_specialty").getValue();

if(lookupItem[0] != null)
{
    var name = lookupItem[0].name;
    var guid = lookupItem[0].id;
    var entType = lookupItem[0].entityType;
}

org.new_Description is the actual field name in the looked-up entity (new_Description).

This way you can start populating current form fields with values from the look-up entity selected by the user. Be aware though that there might be a delay from look-up value change until the call takes place, results are returned and fields are populated. Depending on the environment, this could be visible or instant.

Enjoy!

Microsoft Business Solutions MVP

Check out my course [Video]

Configuring and Extending Dynamics 365 Customer Engagement

Configuring and Extending Dynamics 365 Customer Engagement

Check out my course [Video]

Getting Started with Dynamics 365 Customer Engagement

Reviewed Book

Implementing Microsoft Dynamics 365 for Finance and Operations

Implementing Microsoft Dynamics 365 for Finance and Operations

Reviewed Book

Microsoft Dynamics 365 Extensions Cookbook

Microsoft Dynamics 365 Extensions Cookbook

Check out my Book

Microsoft Dynamics CRM 2016 Customization - Second Edition

Microsoft Dynamics CRM 2016 Customization - Second Edition

Check out my Book

Microsoft Dynamics CRM Customization Essentials

Microsoft Dynamics CRM Customization Essentials

Check out my Book

Microsoft Dynamics CRM 2011 Scripting Cookbook

Microsoft Dynamics CRM 2011 Scripting Cookbook

Reviewed Book

Microsoft Dynamics CRM 2011: Dashboards Cookbook

Microsoft Dynamics CRM 2011: Dashboards Cookbook

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 599 other followers

Follow Dynamics 365 Wizardry on WordPress.com