Make all the required fields red

In CRM 2011 we have all these jQuery stuff to play with, so I have created a generic function to make all the required labels which are not filled in Red. Then when they are filled in, they switch back to black.

Here you can see the function, first field is filled, and is black. Second is yet to be filled, hence Red.

Required Fields Check

 

This is simpler then you might think, however the SDK examples are a bit too simple to have this support. Since I want to make my function generic. I don’t want to have to add my function to every field that is required. This is how I did it:

 

if (typeof (stq) == "undefined")
{ stq = {}; }

stq.SaveCheck = {
    AttachOnLoad: function () {
        var attributes = Xrm.Page.data.entity.attributes.get(function (attribute, index) {
            return (attribute.getRequiredLevel() == "required")
        });

        for (var i in attributes) {
            var attribute = attributes[i];
            attribute.addOnChange(stq.SaveCheck.attributeChanged);
            attribute.fireOnChange();
        }
    },

    attributeChanged: function (context) {
        var name = context.getEventSource().getName();
        var field = Xrm.Page.getAttribute(name);
        if (field.getValue() == null)
            stq.SaveCheck.setLabelColor(name, "Red");
        else
            stq.SaveCheck.setLabelColor(name, "Black");
    },

    setLabelColor: function (fieldName, color) {
        $("#" + fieldName + "_c > label[for=" + fieldName + "]").css("color", color);
    }
};
This entry was posted in CRM 2011, Customizations, MS CRM and tagged , . Bookmark the permalink.

2 Responses to Make all the required fields red

  1. Joshua says:

    do you know how to change the background color of a text field (not label, but field)?

    • Markus says:

      it’s just a minor change:
      exchange
      $(“#” + fieldName + “_c > label[for=" + fieldName + "]“).css(“color”, color);
      with
      $(“#” + fieldName).css(“background”, color);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s