CRM 4.0 javascript to autopopulate a field on save

We have a field that is required but if left blank on save we want it to autopopulate with the combination of two fields that exist on the form. I'm guessing this can be done in javascript. Any ideas? Thanks
biotecAsked:
Who is Participating?
 
crm_infoConnect With a Mentor Commented:
Don't forget to test to make sure that the field you are putting values into is null before running this most recent script - otherwise you'll overwrite your existing value.  Here is revised script to handle that for you as well.
As an aside, you can also update the field in "real time" as the user enters the other two fields.  When they come to the field that contains the data, they can either leave it alone or make changes.  Submit a second question if you want details on how to do that and I'll update the script accordingly.

if (crmForm.all.[VarCharFieldName].DataValue == null) {
 
// Retrieve the value of the lookup field
// If it is null, then return an empty string
var lkupfield = crmForm.all.[LookupFieldName].DataValue;
if (lkupfield[0] != null) {
    var lkupvalue = lkupfield[0].name;
}
else
{
    var lkupvalue = "";
}
 
// Return the value for the Picklist item
var picklistvalue = crmForm.all.[PickListFieldName].SelectedText;
 
// Concatenate the two values into a third value - a varchar attribute
crmForm.all.[VarCharFieldName].DataValue = lkupvalue + " " + picklistvalue;
 
}

Open in new window

0
 
crm_infoCommented:
Yep - it's very possible.  Exactly how you do this will depend upon the field types (i.e. Picklists and Lookups require some special treatment).  For now, I'm assuming that you're going to be merging two text fields together to do this.

I'll summarize the process and attach some code.   If you need more details just let me know and I'll send that along.

(1) First of all, you'll need to open the form for the record that you want to update:  In the Web version of CRM: Settings | Customization | Customize Entities | Choose the entity you want this customization to work for | Double Click | Click the Forms and Views item on the side-nav | Double Click on the Form.

(2) I'm assuming that the two fields you want to use to create the third field, and the third field itself, are all on the form.  If not, then go ahead and add them to the form.

(3) Now you're going to update the OnSave Event: Click Form Properties (right-side of the form, bottom) | Select OnSave and then Edit | Make sure you check the "Event is enabled" Checkbox  | use the sample code, below, as a starting point to develop your own code.

(4) Click OK, Click OK again

(5) From here, you can test your code if you like by using the preview button

(6) Once you're satisfied that the form works the way you like, you can click Save and Close, Save and Close again, then, on the list of all entities, click the Publish button.

Your form should now work the way that you want.

WARNING: JavaScript is case sensitive, make sure you have your case correct.  Again, depending upon the types of fields that you are grouping together, this may involve more code than I've place below - let me know if it works for you and, if not, let me know the names of the fields you're workign with and the types of fields and I'll try to give you the specific code you'll need to get this working.


if (crmForm.all.jobtitle.DataValue == null) {
  crmForm.all.jobtitle.DataValue = crmForm.all.firstname.DataValue + " " + crmForm.all.lastname.DataValue;
}

Open in new window

0
 
biotecAuthor Commented:
This is real close and I appreciate all the detail here. I'm getting back;

[object Object] 2

So I'm thinking that since I'm pulling one value from a pick list and the other is from a lookup field that maybe that is why it's not grabbing the actual data. Any help is greatly appreciated. Thanks
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
crm_infoCommented:
Yep, lookup fields are a buggere to work with.  Picklists are alao unique.  Here is what you should do:
To get a value out of a Picklist, don't use DataValue - use SelectedText.  Such as:
crmForm.all.fieldname.SelectedText
Lookup fields actually store a number of different values in an array.  Put another way, they're a pain in the tookas to work with!  
The code snippet below will take a lookup value and a picklist value and concatentate them into a third field of time varchar.  Substitute your field names in there and I think you'll have what you need.

// Retrieve the value of the lookup field
// If it is null, then return an empty string
var lkupfield = crmForm.all.[LookupFieldName].DataValue;
if (lkupfield[0] != null) {
    var lkupvalue = lkupfield[0].name;
}
else
{
    var lkupvalue = "";
}
 
// Return the value for the Picklist item
var picklistvalue = crmForm.all.[PickListFieldName].SelectedText;
 
// Concatenate the two values into a third value - a varchar attribute
crmForm.all.[VarCharFieldName].DataValue = lkupvalue + " " + picklistvalue;

Open in new window

0
 
biotecAuthor Commented:
This is probably the single best most detailed and quick responses I've had on this site since I started using it about 5 years ago. Awesome!
0
 
crm_infoCommented:
Glad it helped.  And thanks for showing me the love!
0
All Courses

From novice to tech pro — start learning today.