How to replace Lookup empty Name, with different field value

I just started working with a new customer and we already have some interesting challenges, some of the bulk import data, have most of the Contact Entity records empty except with on field that was an representation from other system called the Customer Number.

Well when CRM displays the Contact information is really nothing, just have the 5000+ records count and nothing on the screen, well I fixed that adding the Customer Number to the Views, but then another issue, in a relation with other entity the Contact relays in the Full name to display as part of the Lookup field, but there is no Full name on the record and can’t be clicked to take you back to the Contact record.

The solution is to have client side code that will run on the Onload event of the form, get the current Lookup information, make a request to get the Contact record information using oData, and reassign the Lookup Name value with other field on the Contact Entity.

The following is the code used to accomplish the solution on the related entity. You need to add the function “changeCustomerLookup()” to the form onload event.
Also you will need the following Files to be included in your form libraries:

  •  abe_jason2.js
Download
  • abe_SDK.REST.js
Download
  • abe_SDK.JQuery.js
Download

 

function changeCustomerLookUp() {

var ExistingContact = Xrm.Page.data.entity.attributes.get("abe_contact");
    if (ExistingContact.getValue() != null) {
        var ExistingGUID = ExistingContact.getValue()[0].id;
        var _ToEntity = "Contact";
        SDK.REST.retrieveRecord(ExistingGUID, _ToEntity, "*", null,
        function (result) {
            if (result.LastName == null) {

                var ExistingName = result.abe_customernumber;


     SetLookupValue("abe_contact", ExistingGUID, ExistingName, "contact");

            }

        }, function (error) { alert('Contact Error:' + error.message) });


    }
}

function SetLookupValue(fieldName, id, name, entityType) {
    if (fieldName != null) {
        var lookupValue = new Array();
        lookupValue[0] = new Object();
        lookupValue[0].id = id;
        lookupValue[0].name = name;
        lookupValue[0].entityType = entityType;
        Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
    }
}

Downlod the Client side code here: abe_LookupEdit.js Download

enjoy the code, please leave your comments and recomendations

 

Abe Saldana

CRMBuzz.

Get membership Access to all Content!

Get Access to the most updated content for Dynamics 365 and other technologies, receive emails when we post New Content and when we have new Videos, and Live Sessions! Subscribe to get our latest content by email. Also to get and download the source code on the new blog post!

We won't send you spam. Unsubscribe at any time. You will receive emails from AbeSaldana.com Powered by ConvertKit

Leave a Reply

Your email address will not be published. Required fields are marked *

Blue Captcha Image
Refresh

*