0
votes

I have the following situation in Dynamics CRM 2016.

I am trying to create a JS web resource that will act on a dropdown field change.

function esitoappuntamento() {
  Xrm.Page.getAttribute("new_esito").addOnChange(myOnChange);

  function myOnChange() {
    if (Xrm.Page.getAttribute("new_esito").getSelectedOption().value == "100000006") {
      Xrm.Page.getControl("new_esitoapp").setVisible(false); 
      Xrm.Page.getAttribute("new_esitoapp").setRequiredLevel("none");
    }
    else {
      Xrm.Page.getControl("new_esitoapp").setVisible(true);  
      Xrm.Page.getAttribute("new_esitoapp").setRequiredLevel("required");
    }
  }
}

I have the option set field new_esito with options/choices. What I want to do is to show the new_esitoapp field only when the form loads and the new_esito value is 100000006 and someone changes the value from 100000006 to something else from the remaining 7 choices.

Otherwise if when the form loads the new_esito value is not 100000006, it should not show the new_esitoapp field independently from what I choose in the new_esito field from the 8 available choices.

This is working partially as I need it to work, because it is working fine for the case when the form loads and the field value is 100000006.

The problem is that it is still showing the new_esitoapp field even when the form loads and the value of new_esito is not 100000006 and I change it to something else.

So the behavior should be:

Form load -> new_esito = 100000006 -> change new_esito -> show new_esitoapp

Form load -> new_esito != 100000006 -> change new_esito -> no show new_esitoapp

Wrong behavior now is:

Form load -> new_esito != 100000006 -> change new_esito -> show new_esitoapp

I hope I made myself clear about what I want to achieve.

Looking forward to your replies.

Regards

2
Are you calling this myOnChange function on form load or not? Atleast fireOnChange() will help you..Arun Vinoth - MVP
This shouldn't matter since you're using == rather than === but the Option Set value in Turbo Forms are ints, not strings.Daryl

2 Answers

0
votes

You should fire your show\hide action on OnLoad of the form. More details here:

Yours function myOnChange should be triggered both on OnLoad of the form and on OnChange of your new_esito attribute.

The other option is to utilize business rules:

0
votes

In the above code, you are just writing an eventhandler myOnChange and binding it to onChange event of new_esito using addOnChange.

When you call this esitoappuntamento on form load, its bound & ready to fire next time when change happens.

But you are not triggering it until unless, you fire that using fireOnChange. All these are dynamic way of doing on the fly.

The other way of binding this is in form editor, adding the onChange event under field properties. Which will bind it to control & trigger it during form onload itself.