locked
CRM 2011 Trying to change Require Level of one field based on value in another. RRS feed

  • Question

  • Hello-

    I am trying to to change the required level on a field based on the value in another field.  For example a Lookup field is Required.  I want to change the required level to none if 'Research' is the value selected in an Option Set field.  Thi is not working.  What am I doing wrong?

    function Form_OnSave() {
     }
     function Form_OnLoad() {
         setRequire()
     }
     function setRequired() {

    var type = Xrm.Page.getAttribute("cprs_type").getSelectedOption().text;

         if (type == "Option5") {
             // If the value is Research set field primarypractice areas required level as none

             Xrm.Page.getAttribute("cpdc_primarypracticearea").setRequiredLevel("none");
         }
         else

        {
             Xrm.Page.getAttribute("cpdc_primarypracticearea").setRequiredLevel("required");

             // if value is not Research then set primary practiceareas field required level as required

         }
     }

    Monday, November 16, 2015 7:45 PM

All replies

  • Hi,

    What sort of errors do you get? What is type set to? Have you tried to debug it either with developer's console or spam with alerts?

    Regards


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Monday, November 16, 2015 8:05 PM
  • You're missing a semi-colon in the OnLoad event, which might cause syntax errors

    function Form_OnLoad() {
          setRequire();
      }


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, November 17, 2015 11:24 AM
    Moderator
  • HI,

    Call the function properly that's it.

    setRequired();

    Wednesday, November 25, 2015 5:13 AM
  • in addition to the semicolon missing, there is a problem with getSelectedOption().text

    if that optionset has no value you get an error, use .getText instead:

    function Form_OnSave() {
    }
    function Form_OnLoad() {
       setRequire();
    }
    function setRequired() {
    var type = Xrm.Page.getAttribute("cprs_type").getText();
    if (type == "Option5") {
    // If the value is Research set field primarypractice areas required level as none
       Xrm.Page.getAttribute("cpdc_primarypracticearea").setRequiredLevel("none");
    } else {
       Xrm.Page.getAttribute("cpdc_primarypracticearea").setRequiredLevel("required");
    // if value is not Research then set primary practiceareas field required level as required
    }
    }
    another best practice is to use the value of the optionset (using getValue) instead the label, because when you work with more languages (or if someone change the label) the script will still work.


    My blog: www.crmanswers.net - CRM Theme Generator

    Wednesday, November 25, 2015 6:47 AM
  • You are missing d while calling. it should be setRequired not setRequire. just call your method name.

    function Form_OnLoad() {
       setRequired();
    }

    Wednesday, November 25, 2015 1:05 PM
  • Please don't user Javascript here instead use business rule.

    Regards Faisal

    Thursday, November 26, 2015 11:23 AM