Default Customer Lookup to Contact in CRM 2011

There has been a slight change in the way you default the Customer Lookup field to Contact in CRM 2011 compared with CRM 4.

Use the following code to set the Customer Lookup to Contact instead of Account and set the default view.


document.getElementById("customerid").setAttribute("defaulttype", "2");
Xrm.Page.getControl("customerid").setDefaultView("a2d479c5-53e3-4c69-addd-802327e67a0d");

The first line sets the default entity type to Contact for the Customer Lookup dialog.

The second line sets the Customer Lookup dialog’s view to the ‘Contacts Lookup View’ using its guid value. If you do not use this line of code it will default to the ‘My Active Contacts View’ or whatever you have selected as the default Contact view.

Advertisements

51 thoughts on “Default Customer Lookup to Contact in CRM 2011

  1. Janet Thomas

    That is quite useful. But is there a way to limit this lookup so they cannot select an Account -i.e. remove it entirely? We want all Opps to only be associated to contacts. If I use the create new opp from within the Account Opportunity associated view, it defaults to the account and the lookup default is Account. I know that I could use a new custom link to Contacts and not use the vanilla Potential Customer but we are several weeks (and 20K Opps) down the road so I’d rather not have to convert to a new field. We use Outlook client and convert emails to Opps too and that is mapped to Potential Customer so another reason why I don’t want to change fields.

    Reply
    1. Rhett Clinton Post author

      You can try the following

      document.getElementById("parentcustomerid").setAttribute("lookuptypes", "2");
      document.getElementById("parentcustomerid").setAttribute("lookuptypenames", "contact:2");
      document.getElementById("parentcustomerid").setAttribute("lookuptypeIcons", "/_imgs/ico_16_2.gif");
      document.getElementById("parentcustomerid").setAttribute("defaulttype", "2");

      or if using jQuery

      $("#parentcustomerid").attr("lookuptypes", "2");
      $("#parentcustomerid").attr("lookuptypenames", "contact:2");
      $("#parentcustomerid").attr("lookuptypeIcons", "/_imgs/ico_16_2.gif");
      $("#parentcustomerid").attr("defaulttype", "2");

      Reply
      1. Ram

        Hi Rhett,
        Is it possible to clear all the public views listed in potential customer lookup [for both Contact/Account] ?
        I want only the custom view to be available for the end-user.

  2. Rhett Clinton Post author

    Do you get an error? Maybe if you copied and pasted then the double quotes may have got messed up. I just set them using " in my blog post html so hopefully it should resolve it self if that was the problem. You can of course just update your version by replacing the quote characters manually and see if that works.

    Cheers,
    Rhett

    Reply
  3. Ian

    love it, excellent piece of work.
    Do you know if its possible to perform the same thing, but on a sub grid?

    i.e. dynamically change the view used in the form’s subgrid

    cheers
    ian

    Reply
  4. Pingback: A Configuration Data Framework for CRM 2011 « Gareth Tucker's Microsoft CRM Blog

  5. Jamie

    I am new to javascripting in CRM2011, where would I put this code to make these changes, specifically to add this to the opportunity entity?

    Reply
      1. Jamie

        Rhett,

        Thanks for the reply. That is what I thought but I was receiving the following error message:

        ‘document.getElementById(…)’ is null or not an object.

      2. Steve

        On which object do I place this client side code? i have tried it on a case OnLoad event but I just get a form error. Where is the ‘Customer Lookup’ form so I can add it to its onload event?

        Thanks,

      3. Rhett Clinton MVP Post author

        This code is for CRM 2011, and should be placed in the onload event script. In the code, ‘customerid’ is the Customer Lookup field and the guid ‘a2d479c5-53e3-4c69-addd-802327e67a0d’ is the desired Contact View id.

        I hope this helps, cheers
        Rhett

      4. Guido

        Hi Rhett
        i cant’ find the form owner of the onload event.

        for example i want it to use to select a Contact (instead of an Account) in the Regardinf of an Appontment.

        which onload event i have to use? not the one of the Appointment, i suppose.
        thanks!

      5. Damian

        Hi Rhett,
        I am a newbie and am having issues with this. Similar to the comment made by Steve – “On which object do I place this client side code? i have tried it on a case OnLoad event but I just get a form error. Where is the ‘Customer Lookup’ form so I can add it to its onload event?”

        The error I get is “The value off the property ‘setContact’ is null or undefined, not a Function object.” I am trying to apply this code to the Case entity form via the client in the solution section. It would seem this would have to be applied on a Customer Lookup form which seems to me to be the actual onLoad event, but I can’t find this form anywhere in the solution. Do I need to be doing this via Visual Studio?

      6. Rhett Clinton MVP Post author

        Hi Damian,

        You need to create an onload function in a js web resource and then attach that to your Case form onload event. Inside the onload function you can call this code. This code alters the default view that the Lookup field will load in the custom dialog when you click the Lookup field button.

        I hope that helps.

        Rhett

      7. Damian

        Hi Rhett,
        This is what I’ve been doing but to no avail. Here are my steps, I’m not sure if i’m just missing something very basic or if it has something to do with my CRM environment. I’m running CRM 2011 On-premise with rollup 6.

        1. Created Jscript web resource file called new_Employee (this particular org is for HR, renamed contact entity to employee)
        2. Added following code to .js web resource
        function setLookupEmployee()
        {
        document.getElementById(“customerid”).setAttribute(“defaulttype”, “2″);
        Xrm.Page.getControl(“customerid”).setDefaultView(“00000000-0000-0000-00AA-000010001004”);
        }
        I have also tried this without the second line of code to see if it defaults to the default view but get the same error.

        3. Added web resource to Case entity form as available library
        4. Added setLookupEmployee to Form onLoad event.
        5. Published All Customizations

        When I open a new case record i get an error: “There was an error with this field’s customized event.
        Field: window
        Event: onload
        Error: The value of the property ‘setLookupEmployee’ is null or undefined, not a function object.”

        Not sure what I’m doing wrong. There MS error log show a line about an invalid character as below but I don’t know what exactly this is referring to:

        1.0

        Invalid character
        3
        /%7B634708046070000000%7D/WebResources/new_Employee
        /userdefined/edit.aspx?etc=112&pagemode=iframe&preloadcache=1335207843180

        Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3; Tablet PC 2.0; MS-RTC LM 8)
        en-us
        en-us
        en-us
        1280×800
        Web
        2012-04-23T12:04:03

        1033
        1033
        1033
        1033
        {EA55B557-0972-E111-8D30-0050569F003E}
        {6E5F5E0F-0C72-E111-8D30-0050569F003E}
        5.0.9690.1992

      8. Rhett Clinton MVP Post author

        Try replacing the quotes in the code by typing them again as they may have got messed up if you copy/pasted that line of code, they could be the invalid characters.

      9. Damian

        That was it, works beautifully now. I retyped the quotes, I guess as you say they didn’t copy over as they appeared. I mentioned this to one of my colleagues in passing and she laughed at me saying “Ah yes, the dreaded quotes”. I guess this is a common rookie mistake when copying code from a web page.

        Thank you so much for your help.

  6. Atul

    Hi Rhett,

    I am using following code to freeze lookup

    document.getElementById(“parentcustomerid”).setAttribute(“lookuptypes”, “2”);
    document.getElementById(“parentcustomerid”).setAttribute(“lookuptypenames”, “contact:2”);
    document.getElementById(“parentcustomerid”).setAttribute(“lookuptypeIcons”, “/_imgs/ico_16_2.gif”);
    document.getElementById(“parentcustomerid”).setAttribute(“defaulttype”, “2”);

    it works fine, but the script after this code is not working.
    as i’m trying to freeze “Regarding” field of any activity,and after that i’m setting some value for that field,that time it not sets the field value.

    Regards,
    Atul

    Reply
      1. Atul

        Hi Rhett,

        I am using this code on “onload” event of my form.
        E.g
        On Contact form OnLoad event i’m freezing Account lookup only to Account.
        after that i am doing onchange event on another field. but it doesn’t fires that onchange event. if i removes this code then it works.

        Regards,
        Atul

  7. Michał

    Hello Rhett,
    I have issue with this code:

    function form_onLoad()
    {
    hideContactOnCustomeridField();
    }

    function hideContactOnCustomeridField()
    {
    document.getElementById(“customerid”).setAttribute(“lookuptypes”, “1”);
    document.getElementById(“customerid”).setAttribute(“lookuptypenames”, “account:1”);
    document.getElementById(“customerid”).setAttribute(“lookuptypeIcons”, “/_imgs/ico_16_1.gif”);
    document.getElementById(“customerid”).setAttribute(“defaulttype”, “1”);
    }

    It is run on opportunity. When I am creating new opportunity everything is ok (I can fill/change customer field only to account), but after saving when I try to change customerid field to other account, customerid field is empty (is not filled) and old value is removed.

    Best regards,
    Michał

    Reply
  8. Brenda

    Thanks for the info on this. However, I put the code in the onload event and get a generic error when I click on the lookup button on the Case entity.

    Here’s what I put in the Onload event on the Case entity – working with CRM 2011 – Online. Grabbed the default view guid from the address bar when I clicked on the lookup (without the event enabled). I also put the code in Visual Studio to verify quotes – all looks good.

    I’m guessing it’s having a hard time with the guid. I did try using the guid mentioned in this blog, but got the same results.

    function setLookupContact()
    {
    document.getElementById(“customerid”).setAttribute(“defaulttype”, “2”);
    Xrm.Page.getControl(“customerid”).setDefaultView(“%7bA9AF0AB8-861D-4CFA-92A5-C6281FED7FAB%7d”);
    }

    Your help is appreciated.

    Brenda

    Reply
      1. Brenda

        Hi Rhett,

        Thanks – I finally figured it out. Haven’t been able to set the default view however, but at least the lookup defaults to Contacts. Fortunately the users want the system defaulted view so I’m good.

        Thanks.

        Brenda

      2. Rhett Clinton MVP Post author

        Hi Brenda, I think if you had removed those characters I mentioned and if you had the right view guid (which you can get by opening one in the customisations area of CRM and viewing the address bar, which I think you already did..) then it should work.

        Cheers,
        Rhett

  9. Jeremy

    Rhett,

    I am using this code to set the “to” field on a phone call to default to Contacts. It works just fine on the initial lookup click. But if you I open the advanced find, change the picklist from contacts to accounts and then click back on contacts the advanced find stays on account records.

    Reply
    1. Jason Heiser (@breaker119)

      Jeremy, did you change the element ID or anything from the code snippet? All I did was add “function setLookupContact()” and braces. I can’t even get the initial lookup to default to Contacts. It’s still defaulting to Accounts.

      Reply
  10. Surendra Iyer

    Rhet,
    I am using this code to set the “to” field on a Email Activity to default to Contacts and User. It works just fine on the initial lookup click. when a person decides to change from the new default lookup (say Contacts) to another lookup (User) then back to the original (Contacts). Then there is an error of “This object does’t support this property or method”.
    Below is my javaScript, which is in the Onload
    crmForm.all.to.setAttribute(“lookuptypes”, “2,8”); //only show contact and System User
    crmForm.all.to.setAttribute(“lookuptypeIcons”, “/_imgs/ico_16_2.gif :/_imgs/ico_16_8.gif”); // set Icons
    crmForm.all.to.setAttribute(“defaulttype”, “2”); // set Default.

    Please help me How I can get rid of this error

    Reply
  11. skfd

    Can this approach be applied to default lookups like products on opportunity product? This code did nothing in onChange:
    Xrm.Page.getControl(“productid”).setDefaultView(’68C9758D-155E-E111-8C0E-0800277B32DC’);

    Reply
    1. Rhett Clinton MVP Post author

      You need to obtain the view id that you want to display, I am not sure of any reason why this view will not default for you if you have a valid view id.

      There is a issue with Connections entity where by the defautt view is set internally upon clicking the Lookup button which prevents the default view from being set and this may apply elsewhere though I have not come across other scenarios like this.

      Regards,
      Rhett

      Reply
      1. skfd

        Here’s what I tried.
        I copied Id of custom Products view and added following code, which did nothing.
        “””
        $(“#productid”).attr(“disableViewPicker”, “0”);
        Xrm.Page.getControl(“productid”).setDefaultView(‘A066D18A-8A4A-44EE-B78E-9A870DC799DD’);
        “””
        Ended up building View with addCustomView() in js code, but that’s no big deal.

      2. Joakim

        Hi Rhett,
        Do you know if there is a way to set the default view in Connections?
        I have also tried addCustomView with no luck 😦
        Regards,
        Joakim

  12. Surendra Iyer

    Thanks Rhett,

    The Code below works in MSCRM 2011 for setting default contact and user in the party list.

    crmForm.all.to.setAttribute(“lookuptypes”, “2,8”); //show contact and user
    crmForm.all.to.setAttribute(“lookuptypeIcons”, “/_imgs/ico_16_2.gif :/_imgs/ico_16_8.gif”);//set the icons
    crmForm.all.to.setAttribute(“defaulttype”, “2”);
    Xrm.Page.getControl(“to”).setDefaultView(“00000000-0000-0000-00AA-000010001003”);

    Regards
    Surendra Iyer

    Reply
  13. CRM 2011 developer

    I did
    crmForm.all.to.setAttribute(“lookuptypes”, “2″); //show contact and user
    crmForm.all.to.setAttribute(“defaulttype”, “2″);

    and the default view is My Active Contact, but the filter won’t be triggered to show only my active contacts,it shows all contacts. How to trigger the filter?

    Reply
  14. Rohit

    I have two entities “Template” and “Products”.. I want to create a lookup in template entity for grouping products.. I have crated a lookup field in template form but it is selecting only one product, how can I enable lookup to allow multiple product selection???

    Please help

    Reply
      1. Rohit

        thanks for replying… I have done it, but I have an another question..

        How can I retrieve all the “products” related to a “template”, as a “template” can have multiple “products”.

        It is like an “Account” can be related to multiple “Opportunities”. I want to retrieve all the “Opportunity” related to an “Account” entity.

        “Product” and “Template” are custom entity I created.

        Is there any way achieving the task using C#?.

        Any help will be appreciated… thanks.

        Rohit

  15. Pingback: CRM 2011 – Good list of Javascript articles | Roman's Blog

  16. Muahmmad Sohail

    this code is not running for CRM 2013 plz how we change or update it for CRM 2013
    as in CRM 2013 DOM ahs been changed.
    document.getElementById(“customerid”).setAttribute(“defaulttype”, “2”);
    Xrm.Page.getControl(“customerid”).setDefaultView(“a2d479c5-53e3-4c69-addd-802327e67a0d”);
    plz update it for CRM 2013

    Reply

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 )

Google+ photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.