none
Conditionally required field?

    Întrebare

  • First of all, I have Finnish version of Dynamics CRM 2011 On-premise, so the terms might be a bit off (go ahead and correct me).

    Anyway, I was required to change fields of forms to be conditionally required so how can I do this? As an example, let's say we have a field "final sale amount", which ultimately needs to be filled, but at the time we create a deal, we have no idea how much the final sale amount is. We can't use something like 0, because that screws up the averages etc. and we certainly can't put there "unknown", since it has to be numeric.

    So what I would need is a conditional requirement level like this pseudocode:

    if StateOfSale = Finalized then

    form.Fields.FinalSaleAmount.Requirementlevel = Required

    else

    form.Fields.FinalSaleAmount.Requirementlevel = Not Required

    end if

    Where and how do I input that?

    7 septembrie 2012 11:45

Răspunsuri

  • You'll have to tweak this code a bit - the intial 'IF' statement might change depending on what type of field it is (text, option set, 2 options, etc...)

    function OnChange() {
        if (Xrm.Page.getAttribute("yourStateField").getValue() == "Finalized") {
            Xrm.Page.ui.controls.get("yourFinalSaleAmt").setRequiredLevel("required");
        }
        else {
            Xrm.Page.ui.controls.get("yourFinalSaleAmt").setRequiredLevel("none");
        }
    }

    This code needs to be added into a web resource file (example creating) and the OnChange function in this code need to be tied to the OnChange event of the field you want to control setting the requirement (StateOfSale).

    Hopefully this points you in the right direction.


    Jason Lattimer

    7 septembrie 2012 12:57

Toate mesajele

  • You'll have to tweak this code a bit - the intial 'IF' statement might change depending on what type of field it is (text, option set, 2 options, etc...)

    function OnChange() {
        if (Xrm.Page.getAttribute("yourStateField").getValue() == "Finalized") {
            Xrm.Page.ui.controls.get("yourFinalSaleAmt").setRequiredLevel("required");
        }
        else {
            Xrm.Page.ui.controls.get("yourFinalSaleAmt").setRequiredLevel("none");
        }
    }

    This code needs to be added into a web resource file (example creating) and the OnChange function in this code need to be tied to the OnChange event of the field you want to control setting the requirement (StateOfSale).

    Hopefully this points you in the right direction.


    Jason Lattimer

    7 septembrie 2012 12:57
  • You'll have to tweak this code a bit - the intial 'IF' statement might change depending on what type of field it is (text, option set, 2 options, etc...)

    function OnChange() {
        if (Xrm.Page.getAttribute("yourStateField").getValue() == "Finalized") {
            Xrm.Page.ui.controls.get("yourFinalSaleAmt").setRequiredLevel("required");
        }
        else {
            Xrm.Page.ui.controls.get("yourFinalSaleAmt").setRequiredLevel("none");
        }
    }

    This code needs to be added into a web resource file (example creating) and the OnChange function in this code need to be tied to the OnChange event of the field you want to control setting the requirement (StateOfSale).

    Hopefully this points you in the right direction.


    Jason Lattimer

    At least it sounds about right. Thanks!

    7 septembrie 2012 13:09
  • Hi KlipAr,

    Try below

    if (Xrm.Page.getAttribute("StateOfSale").getValue() == "Finalized") {
       Xrm.Page.getAttribute("FinalSaleAmount").setRequiredLevel("required");
        }
        else {
       Xrm.Page.getAttribute("FinalSaleAmount").setRequiredLevel("none");
        }

    Regards,

    -Anand

    7 septembrie 2012 14:42