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;
}
Comments
Anonymous
May 11, 2008
Just an FYI. Because it's client side scripting, you can't be assured that the name field will be updated by bulk edits, workflow updates, plugin updates, etc... It will only take place when a manual entry is made on the form.Anonymous
May 13, 2008
We do this in precreate and preupdate callouts (3.0) and plugins (4.0) to account for the scenario Jon G points out above. We've toyed with the idea of doing as workflows in 4.0 to make it more transparent to the client, but we're afraid of the overhead.Anonymous
May 16, 2008
This is an alternative method of doing the same thing we talked about with this post.   John G fromAnonymous
July 05, 2008
The comment has been removedAnonymous
July 28, 2008
Hi, I found this type of scripting to be very usefull, but tricky. Because as in the example the total length of what you're trying to put in the field may be longer than the field you're trying to put it into. So to avoid errors, I've changed the script to trim the value: //Generate the value for the name field var Eventname = ''; var Employeename = ''; var EnrolmentnameConcat = ''; //Set Eventname if (crmForm.all.wfv_educationassessmentid.DataValue != null) { Eventname = crmForm.all.wfv_educationassessmentid.DataValue[0].name; } //Set Employeename if(crmForm.all.wfv_employeeid.DataValue != null) { Employeename = crmForm.all.wfv_employeeid.DataValue[0].name; } //Create value before lenght check EnrolmentnameConcat = "of " + Employeename + " for " + Eventname; //Set name value with max field length crmForm.all.wfv_name.DataValue = EnrolmentnameConcat.substring(0,99);Anonymous
October 28, 2008
This is an alternative method of doing the same thing we talked about with this post.   Jon G from