We help IT Professionals succeed at work.

CRM 4.0 javascript to autopopulate a field on save

biotec
biotec asked
on
Medium Priority
6,232 Views
Last Modified: 2012-06-27
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
Comment
Watch Question

Commented:
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

Author

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

Commented:
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

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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!

Commented:
Glad it helped.  And thanks for showing me the love!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.