Tuesday, 26 August 2014

CRM 2011 - Changing the color of Text fields

We had a scenario where we wanted to clearly identify the differences between two addresses on a form.

To facilitate this we thought we would change the text color however we hit two issues
1) Most of the examples showed an unsupported example.
2) It didn't seem to work when the field was read only

The examples seem to use the standard JavaScript style
document.getElementById("fieldname").style.color = "red";
While my final solution might still not be Microsoft supported, it is a bit more "CRM'd" then the above.
var field = Xrm.Page.ui.controls.get("fieldname");
if (field != null) {
     field._control.get_element().firstChild.style.color= 'red';
}

HOWEVER as the display of read only fields is defined by the client browser/css modifying this would be tricky at best. So instead we changed the background color which will work on a disabled field.
var field = Xrm.Page.ui.controls.get("fieldname");
if (field != null) {
     field._control.get_element().firstChild.style.backgroundColor = 'red';
}
Let me know if you have a more elegant solution!