Dynamics CRM: Set Title Case for a text field using JScript; but only once

Today I had to work on a interesting JScript for Dynamics CRM. When creating a new Contact record, the text values within the fields for First Name, Middle Name and Last Name should have a Title Case enforced to them. For example, if a user types “JOhN” in the First Name field, the case should be automatically corrected to “John”.

To make things more interesting, here is the second part of the requirement: After a case correction has been performed in a field, it shouldn’t be performed again. So if the user now proceeds to type “JOHN” in the First Name field after the first case correction, the system should leave the case as it is.

Now, I am not saying that I agree with this requirement, but it is a requirement nonetheless. I make no secret that I am far from having the JScript knowledge I aspire for, so this was a good exercise for me and I hope the community can benefit from it.

This is an interesting solution because it covers the usage of global variables in JScript, function parameters and an array for individually controlling whether a field should have its case fixed or not.

  1. The first step is to create a new JScript web resource for our Dynamics CRM solution. I called it new_setTitleCase. Then paste the following code for the script:
// Let's start by declaring the Global variable outside the function. Feel free to name it _WhatEverYouFancy
var _i = null;

// This is the function we will call in the onChange event of a field. Make sure to pass the field Schema Name as a parameter (fieldName) when using the function
setTitleCase = function(fieldName) {
  if ((_i != null) && (_i.indexOf(fieldName) >= 0))
    return;
  var CRM_FORM_TYPE = Xrm.Page.ui.getFormType();

  // If the form isn't of type 1 (Create Form) then stop the script
  if (CRM_FORM_TYPE != 1) { return; }

 var str = Xrm.Page.data.entity.attributes.get(fieldName).getValue();

 str = str.toLowerCase().replace(/\b[a-z]/g, function(letter) {
 return letter.toUpperCase();
 });

 Xrm.Page.getAttribute(fieldName).setValue(str);
 if (_i == null)
 _i = fieldName;
 else
 _i += "|" + fieldName;

}
  • Now while customizing the Contact form, click on Form Properties, then under Form Libraries within the Events tab we must add the new JScript we just created. Confirm all the changes and go back to the main Contact customization form.
  • Now double-click on a field we would like the Title Case to be enforced to. Within the Events tab, click on Add under Event Handlers to bind our JScript to the onChange event of the field.
  • In the Handler Properties dialog, make sure that the correct script is specified in Library selection. Type setTitleCase as the value for the Function field, and then type the field schema name between quotes for the parameters field. For example 'firstname' for the First Name field.
  • Make sure to confirm all changes and publish the customizations.

There is no way I could have code this script by myself with my limited knowledge of JScript. Thanks to JBlaeske at the Microsoft forums for the help on this one!


Comments are closed.