Change requirement level at runtime

I've made quite some posts around how to dynamically modify the form by using JavaScript. One thing I haven't posted around yet, but what I do use quite often, is to modify the requirement level of attributes on the form at runtime. Based on a selection in the system, another field could become required. Also if the selection changes again, then the field should not be required anymore. Here is the code for making a field optional, recommended or required:


// Set field to not required
crmForm.SetFieldReqLevel("fieldname", 0);

// Set field to business required
crmForm.SetFieldReqLevel("fieldname", 1);

// Set field to business recommended
crmForm.SetFieldReqLevel("fieldname", 2);


Update: new code placed based on the comment by Peter. Before the update I had this piece of code:


// set the field required (i.e. show error message when field is not filled in)
crmForm.all.fieldname.req = 2;

// modify the label to be red
crmForm.all.fieldname_c.className = 'req';

8 comments:

  1. You can also use this method:

    // Set field to not required
    crmForm.SetFieldReqLevel("fieldname", 0);

    // Set field to business required
    crmForm.SetFieldReqLevel("fieldname", 1);

    // Set field to business recommended
    crmForm.SetFieldReqLevel("fieldname", 2);

    which I personally like more because everything is handled with just 1 line of code instead of 2.

    ReplyDelete
  2. It would be very useful if you could start tagging your posts with the CRM version. Is this likely to be supported in v4 ?

    ReplyDelete
  3. Hi M,

    I wish I could do that, but for now I still am not allowed to say if something is available / supported or not in Titan.

    I would like to share that Titan is just build on top of CRM v3, so most of my posts will also work for Titan.

    Ronald

    ReplyDelete
  4. Oh, I see - I hadn't thought of non-disclosure issues! Thanks.

    ReplyDelete
  5. Hi Ronald,

    hope you may help me on the below issue,

    frequently, crm mailbox is getting full and i need to delete the items to maintain the process flow smoothly.

    how can i prevent the crm mailbox getting full, any ideas appreciated.

    many thanks,
    cem

    ReplyDelete
  6. The field on Form becomes business recommended when I change the related field, but if I remove the value in the related field the other field does not revert back to not required. I need it to change back to not required. I hope you can help and look forward to your answer!

    ReplyDelete
  7. Peter,

    I implemented your script for changing the required field to no constraint and it worked. Great! but when I tried to save the record it throws an error "Invalid Argument". Please help.

    Jimmy

    ReplyDelete
  8. The syntax has changed for 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");

    ReplyDelete

Note: Only a member of this blog may post a comment.