Thursday, February 16, 2012

Custom Lookup with multiple choices in CRM 2011

If you create custom lookup in CRM, this lookup is oriented only for one entity.
I want to add possibility to select from some entities in one lookup.

I have custom lookup "originatingleadid". This lookup is oriented on Lead entity. I want to select from views of three different entities: Lead, Contact, User.

For this I've done some customization in opportunity entity, I've created three hidden text fields:
new_type - to save lookup type
new_guid - to save lookup guid
new_name - to save lookup name.
That is all customization.

I've developed three javascript function:

1. function to handle OnChange event on lookup.

function onchange()
var originatingLeadValue = Xrm.Page.getAttribute("originatingleadid").getValue();
if (IsNull(originatingLeadValue) || originatingLeadValue[0].type == "4")
// Lookup is null or there is a Lead, need to clear hidden fields. To clear one of them will be enough, we will be check this field during onload.
// Lookup contains Contact or User, so need to store lookup properties to our hidden fields.

2. function to handle OnSave event on Opportuniry Form.

function onsave()
var originatingLeadValue = Xrm.Page.getAttribute("originatingleadid").getValue();
if (!IsNull(originatingLeadValue) && originatingLeadValue[0].type != '4')
 3. function to handle OnLoad event on Opportunity Form.

function onload()
lookuptypeIcons = '/_imgs/ico_16_2.gif:/_imgs/ico_16_4.gif:/_imgs/ico_16_8.gif';
lookuptypenames = 'contact:2:Contact,lead:4:Lead,systemuser:8:User';
lookuptypes = '2,4,8';
var savedId = Xrm.Page.getAttribute("new_guid").getValue();
var savedType = Xrm.Page.getAttribute("new_type").getValue();
var savedName = Xrm.Page.getAttribute("new_name").getValue();
var savedEntityName = savedType == "8" ? "systemuser" : "contact";
document.getElementById("originatingleadid").setAttribute("lookuptypes", lookuptypes);
document.getElementById("originatingleadid").setAttribute("lookuptypenames", lookuptypenames);
document.getElementById("originatingleadid").setAttribute("lookuptypeIcons", lookuptypeIcons);
document.getElementById("originatingleadid").setAttribute("defaulttype", "4"); // default type - Lead entity
var originatingLeadValue = Xrm.Page.getAttribute("originatingleadid").getValue();
if (IsNull(originatingLeadValue) && !IsNull(savedId))
var value = new Array();
value[0] = new Object();
value[0].displayClass = "ms-crm-Lookup-Item";
value[0].keyValues = new Object();
value[0].values = new Object();
value[0].onclick = "openlui()";
value[0].id = savedId;
value[0].entityType = savedEntityName;
value[0].typename = savedEntityName;
value[0].name = savedName;
value[0].type = savedType;
So, now you can select from  three different entity in one lookup :-)
Custom Lookup view


  1. This worked PERFECTLY! You're brilliant!

  2. your are a genius man! wonderfull! speechless :D

  3. Hi, simlilar like this, i create a custom view Lead, and i can set it become a default view when i choose entity Lead. But when i choose Contact , an error happen "Entity Name specified in FetchXml does not match the entity name in the EntityExpression",
    Help me please :(

  4. One question. Doesn't that mean that if we look it from another view than a form it will looks like the lookup is empty? I may have misunderstood there. Anyway great post indeed!

  5. Hi it works fine for system entities.

    I need to display two custom entities in one of the custom entity look up view.

    please help me.

    1. Hi Krishna,
      I worked it out for my custom Entities.
      Modify the following as i explained.
      contact:2:Contact--> Here first 1 is entity name, the second one we can get by opening any record for our custom entity and check out the etc value in the url ;) and the third one is the display name for your entity. And i used /_imgs/ico_16_customEntity.gif for the icons to work with. Just modify the code according to your values and it will work like a charm :) for your custom entites as well.

    2. This didn't work for me, an error was thrown when the "Look for" dropdown is changed on the Lookup dialog. Did it work for you with custom entities ?

  6. This comment has been removed by the author.

  7. Hi guys! First of all, thanks all of you for good comments. I'm very happy that my post is helpful for you.
    If you still have some questions about how it works please let me know.

    1. Hi ... Ketevan, I refereed your post and used it for one of my requirement that i need to view Account and contact in Custom look up(By default its a look up for Account)
      But When I choose Contact from the view and select any contact and save the form , the selected Contact Name is not saving.I can only save Account name.That means On Save events its value goes to null. Thsi is the code ,
      function onsave()
      var OwnOrg_Contact = Xrm.Page.getAttribute('new_owningorgcontact').getValue();

      if (!IsNull(OwnOrg_Contact) && OwnOrg_Contact[0].type != '1')

    2. Hi, could you please let me know how to implement this in CRM 2013.

    3. Hi Krishna,

      Was you able to resolve the issue with contact on save?

  8. Ketevan

    I'm pretty new to js and ms dynamics crm. I'm trying to create a custom lookup field on the order form that looks up users or contacts. I think I followed most of your steps correctly, but I don't know what to change the var originatingLeadValue to since I'm using the order form and looking up a user or contact.

    Any thoughts on this? Thank you

    1. Hi,

      Here "originatingLeadValue" is a variable to store your look up field value.You can give any var name(as your wish)

  9. This works, but has a couple drawbacks compared to a standard lookup field. First, when viewing the form and the lookup contains a value, that value is not clickable. I should be able to click on the lookup and have the contact or lead open in a new form. But nothing happens when it is clicked. Do you know any way to make this work?

    A similar problem exists on views. If we put the lookup value on a view, nothing will be displayed. We have to use the 'new_name' value to display the saved value. But again, this is not clickable, like a normal lookup would be on a view. This one would probably be more difficult to fix, but it is less important.

  10. Hi
    any one have tried to save other than default entity records(Lead). Example Contact or User record in custom look up? I tried to save Contact entities records(Contact Name) but its not saving.
    Any Idea?

  11. splendid idea! It works like a charm.

  12. Hi,
    It's working fine for me but do you thinks it's possible to create associated view linked to this entities?

  13. After I installed ms crm 2011 rollup 12, the customized lookup field was un-clickable, and show "error in page".

    I had to check all values for it, and found out that in rollup12,
    value[0].onclick = "openlui()" doesn't works, and MS change it to:

    value[0].onclick = "openlui(new Sys.UI.DomEvent(event))";

    And, the new semantic doesn't work on rollup 10, so, check the rollup version before implementation.

    Hope this information is useful.

  14. Hi,
    Anyone tried on Custom entities !?
    This post is really interesting. I tried to do it for 2 custom entities & it did work till it opened the look up dialog. Once I change the "Look for" drop down to select the second entity, the dialog throws an error. Unfortunately, no detail is provided in the error & says to contact MSCRM administrator & MS.
    I tried to look into other attributes of the look up object, but nothing worked!!
    any idea?

    Thanks !

  15. Hi Ketevan,

    I`ve tried this little workaround to get multiple value lookups in our system. This way is Ok if you just want to choose values of different entities in one lookup, but you cant really work with them if you want to use them "outside" a form.

    1. You can`t save data in a lookupfield if there is no relationship to another entity, so you can`t use this script to show the values of the lookup in a view (you can take one of the hidden text fields, but the data will not be clickable).

    2. For example, you have a lookup oriented to the customers entity on a contract form. using your script, you can choose values from customers and account entity. it`s no problem to work with the data of the contract if you choose a customer, but when you choose an account, theres is no way to get a relationship between the contract and the account.

    is there a way to solve this issues? If not, your script is nice but not really a multi-lookup ;)

  16. I'm looking for the same thinks ^^

    To Srinath NS : it work with custom entities : have change this:

    lookuptypeIcons = '/_imgs/ico_16_1.gif:/_imgs/ico_16_2.gif:/_imgs/ico_16_4.gif:/_imgs/ico_16_4000.gif:/_imgs/ico_16_4009.gif';
    lookuptypenames = 'account:1:Account,contact:2:Contact,lead:4:Lead,mp_custo1:10081:Custo1,mp_Custo2:10082:Custo2';
    lookuptypes = '1,2,4,10081,10082';

    You have to find the type of you custom entities before:

    For this I use this script on the custom form of the custom entities :

    function entitycode() {
    var etc = Xrm.Page.context.getQueryStringParameters().etc;

    It's working fine for me.

  17. This comment has been removed by the author.

  18. Have fill the functions (On load, On Save)on the form (On change) on the field like here :


  19. Thank you so much!!!
    You saved hours of my time with this excellent solution.
    The functions work like a charm!

  20. Did any one fixed the issue. when viewing the form and the lookup contains a value, that value is not clickable. I should be able to click on the lookup and have the contact or lead open in a new form. But nothing happens when it is clicked.

    A similar problem exists on views. If we put the lookup value on a view, nothing will be displayed. We have to use the 'new_name' value to display the saved value. But again, this is not clickable, like a normal lookup would be on a view. This one would probably be more difficult to fix, but it is less important.

  21. HI wat should i specfify the entity number for which i created

  22. Hello Ketevan Dzhgundzhgiya

    Thanks for this custom look up..

    I am facing issue while re-open the same record and try to open related record from the custom look.

    Please let me know if is there any workaround for same.


  23. Hello Ketevan Dzhgundzhgiya,

    I tried this one with CRM 2013 beta, and found that it is not working. Did you tried same or any idea about it.

    Any feedback would be valuable.


  24. Will u please guide me to create multi-select lookup field in CRM 2013

  25. Hello every one
    I want to see the look for = account , not contact, i want to freeze that account by default, don't want to allow to change entities in that filter lookup is customer on case entity

    Please help me Thanks

  26. Please Help me
    When i'm trying to save other than default entity records(Lead) example Contact or User its not saving.