The Pesky Name Field on Custom Entities

When you create a new custom entity by default, it creates a field called Name that is used on lookups, etc. If you don't use the field, it can cause some headaches.  I have been using this as an area to store some information that would make searching on it much easier and automatically fill in the field. (So I don't have to type any more than necessary.)

Shad from Illinois who is a customer who contacted my about some other issues and after pointing him in the right direction for this, he shared back his code for us. :-) Thanks Shad! So in his case he has both the contact and the account linked to the custom entity.  In the OnSave area he put this code. You could put it in On Save or OnLoad or an OnChange.

var lookupItem = new Array;

var contactName = new Array;

// This will get the lookup for the attribute primarycontactid on the Account form.

lookupItem = crmForm.all.new_companynameid.DataValue;

contactName = crmForm.all.new_contactnameid.DataValue;

// If there is data in the field, show it in a series of alerts.

if (lookupItem != null)

{

   // The text value of the lookup.

   crmForm.all.new_name.DataValue = lookupItem[0].name + " - " + contactName[0].name;

crmForm.all.new_name.ForceSubmit = true;

}

else

{

crmForm.all.new_name.DataValue = contactName[0].name;

crmForm.all.new_name.ForceSubmit = true;

}