Link to home
Start Free TrialLog in
Avatar of biotec
biotec

asked on

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
Avatar of crm_info
crm_info
Flag of United States of America image

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

Avatar of biotec
biotec

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of crm_info
crm_info
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of biotec

ASKER

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!
Glad it helped.  And thanks for showing me the love!