Thursday, July 29, 2010

Default Entity for Multi-Entity Lookups

[UPDATE (11/19/2010): I found an example of the method explained in this post at Stunnware’s blog.  I’m a big fan of Michael Höhne and his work, and therefore pay my respects to him for having written about this long before I did.  Perhaps I had even seen it before, but then forgot.]

After hacking the death out of CRM's internal scripting and behavior files, for form controls, I've come to understand some of the innate, undocumented, and probably highly unsupported features that the controls have.  In a recent CRM Developer thread, I found my experience in this area highly valuable.

Lookup fields are pernicious beasts when it comes to customization.  Their tie-in with the Lookup dialog makes them all the more difficult to control.  So, wherever Microsoft has implemented a function that is easy to reuse, I encourage it--often with clear declaration that Microsoft doesn't support such undocumented use.  I'd like to cover more of what I've discovered in an alternate article, but I thought that today I'd share a tidbit:  making Lookup fields that target multiple record types, or entities, default to a particular entity.

The Lookup field exposes a member called defaulttype in the DOM, and its value is set as the Object Type Code representing the Entity which will be selected as default for the Lookup dialog.  For instance, the “To” field on an Email record supports four Entities: Account, Contact, Lead, and User.  The standard default entity, displayed in the “Look For” selection of the Lookup dialog, is the Account (OTC: 1).  If I want to instead set the default as the Contact (OTC: 2), I would use this code in the Email’s OnLoad event:"defaulttype", "2");