2020-01-15
Here's a handy little JavaScript that lets you set a Customer Field in CRM to default to Contact instead of Account. A Customer Field is a "special type" of field in CRM that can either be set to an Account or Contact. When you add a custom one in CRM, it defaults the lookup to go to Accounts first - the user then has to manually (every time!) change it to Contacts if they want to select a Contact record.
There are some client scenarios where having the default as Contact is better. To do that, use the JavaScript below:
function setRepToContact(executionContext) { var formContext = executionContext.getFormContext(); formContext.getControl("new_representative").setEntityTypes(['contact', 'account']); formContext.getControl("new_representative").setDefaultView("{A2D479C5-53E3-4C69-ADDD-802327E67A0D}"); }
and then you can call this function onLoad() for your form in CRM. In the above code, "new_representative" is the name of the customer field. Ensure you use all lower case for 'contact' and 'account' or this script won't work. And just FYI, the Guid A2D479C5-53E3-4C69-ADDD-802327E67A0D is the one for the Contact Lookup View - it is the same in every CRM / Dynamics 365 instance!
For those it might help, here are the steps (this assumes you've already created your custom "Customer" field called new_representative):
Publish all your customizations and try it out!
My name is Rick Towns and I am an amateur astronomer and computer programmer from Canada. This is a collection of interesting posts I've gathered over the years.