Apr 102011
 

This afternoon I was at the Microsoft Dynamics CRM booth at Convergence when someone asked me about required fields in CRM 2011. They wanted to know how to make fields required, but have different forms with different required fields for the same entity.

First, in CRM 2011, when you make a field required by editing the field, you are making it required for all forms (that the field is on).

If you want to make a field required just for a particular form, you can use a form onload event to change the requirement level. One of the great things about CRM 2011 is just how much is exposed to JScript through Xrm.Page. There are methods on the attribute to get the required level and set the required level. Let’s suppose for example that we want to make the First Name required, but only on this form, then we could use the following onload function with the parameters ‘required’, ‘firstname’ to make the field required. If we needed to add multiple fields, then we would just specify them after ‘firstname’, such as ‘required’,'firstname’,'address1_city’.

function updateRequirementLevel()
{
    var level = arguments[0];
    for (var i=1; i < arguments.length; i++)
    {
        var attribute = Xrm.Page.data.entity.attributes.get(arguments[i]);
        attribute.setRequiredLevel(level);
    }
}

That will make each field you specify after the level, required or recommended based on the level you pass as the first parameter. Calling the function updateRequirementLevel('required','firstname'); will make the firstname required.

  10 Responses to “CRM 2011: Required Fields”

Comments (10)
  1. excellent article and exactly what I need to do later today

  2. Good Trick. Thank you.
    But i noticed that there’s few mistakes.
    You’re replacing a Semicolon with a Comma and also Arguments with Fields.

    i edited the code to the following:-

    function UpdateRequirementLevel()
    {

    var level = arguments[0];

    for (var i=1 ; i < arguments.length ; i++)
    {
    var attribute = Xrm.Page.data.entity.attributes.get(arguments[i]);
    attribute.setRequiredLevel(level);
    }

    }

  3. CRM 2011:

    // No Constraint
    Xrm.Page.getAttribute(“field_schema_name”).setRequiredLevel(“none”);

    // Business Required
    Xrm.Page.getAttribute(“field_schema_name”).setRequiredLevel(“required”);

    // Business Recommended
    Xrm.Page.getAttribute(“field_schema_name”).setRequiredLevel(“recommended”);
    CRM 4.0:

    // No Constraint
    crmForm.SetFieldReqLevel(“field_schema_name”, 0);

    // Business Required
    crmForm.SetFieldReqLevel(“field_schema_name”, 1);

    // Business Recommended
    crmForm.SetFieldReqLevel(“field_schema_name”, 2);

    • Thanks Mangesh, I wrote the article as an example of how to set the required level for multiple fields in a single line and I clearly left out the basics of just setting the required level. When I was changing 5 fields to recommended, I just didn’t want to have to go through each one on a separate line, it seemed repetative.

  4. I have noticed in my CRM Online system that if I set a custom field to say ‘Business Required’ through the field properties and if I do not add that field to my existing or new forms, then it really isn’t required for the end user. So, in that case, I can just add the field to the form(s) where that field really needs to be required.

    Could you please confirm whether my statement is correct or maybe there are discrepancies between different systems which could attribute to a platform bug perhaps?

    • You are correct. If the field is not on the form, it is not required, because its not there. This helps prevent post errors and allows you to do a lot more with web-services. If you want to throw an error and make it *truly* required, use a plugin. I updated the post with the (that the field is on). Thanks Ashwin.

      • Thanks for your prompt reply Carlton. Glad we are on the same page now. The Business Required property of CRM fields is only respected for fields that are on the form. It is not respected for creates done through web services or the data import. If it needs to be truly required for all the scenarios, then we’ll have to code it into a PreOperation plugin as you have mentioned.

  5. I noticed that read-only fields are not enforced by required field business rules, any fixes for that?

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>