CRM 2016 E-mails: Defaulting the “To” Field to a Specific Entity

Printer-friendly version

Ever since CRM form rendering has been changed, a lot of unsupported-but-working JavaScript has been rendered useless and alternatives haven’t necessarily been readily available. One example of this would be the method used in CRM 2011/2013 to default the selected entity when the lookup window is opened for a PartyList control, such as the “To” field on the E-mail form. With it, attributes were added directly to a couple HTML elements instead of using anything in the client-side API, which indicated what entities should be available in the entity lookup and which one should be the default. In CRM 2016, though, this no longer works; even though the elements are still on the page, the added attributes are ignored.

One solution is to take advantage of how PartyList controls work: if it is populated with a record, the entity picklist on the lookup window will default to that record’s type. So, if you want a blank “To” field that defaults to Contacts, you can add code on load to populate the field with a dummy reference with a null ID and a space for a name. Of course, you’ll also have to add code to remove the dummy reference before the record is saved, so this alternative is ugly and not ideal.

Another, better, solution is covered in this StackOverflow thread: Lookup contacts instead of accounts on emails in MS Dynamics CRM 2013. In it, the author ran into a slightly different problem with the “To” field in CRM 2013, where he was trying to control which entity displays by default by setting the default view for the control. However, the solution he found works perfectly in CRM 2016 and works as a reasonable replacement for the unsupported code many were using to do the same thing:

function SetDefaultView() {

    var newViewGuid = "{00000000-0000-0000-0000-000000000001}";

 

    var layoutXML =

        ['<layoutxml>',

            '<grid name="resultset" object="2" jump="fullname" select="1" preview="0" icon="1">',

                    '<row name="result" id="contactid">',

                    '<cell name="fullname" width="150" />',

                    '<cell name="parentcustomerid" width="200" />',

                    '<cell name="ownerid" width="100" />',

                    '<cell name="emailaddress1" width="200" />',

                    '<cell name="telephone1" width="100" />',

                    '<cell name="mobilephone" width="100" />',

                    '<cell name="jobtitle" width="100" />',

                    '<cell name="fax" ishidden="1" width="100" />',

                    '<cell name="address1_name" ishidden="1" width="100" />',

                    '<cell name="address1_fax" ishidden="1" width="100" />',

                '</row>',

            '</grid>',

        '</layoutxml>'].join('');

 

    var fetchXML =

        ['<fetchxml>',

            '<fetch version="1.0" mapping="logical">',

                '<entity name="contact">',

                '<attribute name="fullname" />',

                '<attribute name="parentcustomerid" />',

                '<attribute name="telephone1" />',

                '<attribute name="emailaddress1" />',

                '<attribute name="contactid" />',

                '<attribute name="fax" />',

                '<attribute name="address1_name" />',

                '<attribute name="address1_fax" />',

                '<filter type="and">',

                    '<condition attribute="statecode" operator="eq" value="0" />',

                '</filter>',

                '<order attribute="fullname" descending="false" />',

                '<attribute name="mobilephone" />',

                '<attribute name="jobtitle" />',

                '<attribute name="ownerid" />',

                '</entity>',

            '</fetch>',

        '</fetchxml>'].join('');

 

    var ctrl = Xrm.Page.getControl("to");

 

    if (ctrl) {

        ctrl.addCustomView(newViewGuid, "contact", "Custom Contacts Lookup View", fetchXML, layoutXML, true);

        ctrl.setDefaultView("{a2d479c5-53e3-4c69-addd-802327e67a0d}");

    }

}

In the code above, you would change the Fetch XML and Layout XML to suit your needs, if necessary, then make sure that ctrl.setDefaultView is using a valid Contact View GUID. The result is that the Entity picklist will default to Contact when the “To” lookup window is opened, the default View you set will be displayed and the view you added will be available in the list of Contact views.

This is not a perfect replacement for the unsupported method, though, since it leaves the other entity types in the picklist, so the user is free to select records from other entities. The only way to address this, at this time, is to add a prefilter to the control to filter out any entity types that you don’t want the user to select. The entities will still be in the picklist, but the views will not display any records.

About the Author:

TopLine Strategies delivers the complete integration and development of sales, marketing and customer service technologies that enable corporate clientele to improve revenue streams and strengthen customer interactions. Our project management and consulting is designed to achieve timely delivery, 100 percent user adoption of the technologies we implement and deliver measurable returns on investments for our clients.

Comments (0)

Related Blogs

TheReact Native Open Source roadmap was announced in Q4 2018 after they decided to invest more in the React Native open source community.

October is not just about pumpkins, fall foliage, and cooler temps anymore. October 2018 also means the exciting introduction of Microsoft Dynamics 365 for Customer Engagement.

Back in 2016, Microsoft introduced its intentions to refresh its CRM and ERP strategy with Dynamics 365. At the heart of its services was the Common Data Model (CDM).