JS to get details from parent lookup entity

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!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: