Field change event usage

In this recipe we will drill down to a lower level. We have handled form events, and now it is time to handle field events. The following recipe will show you how to bring all these together and achieve exactly the result you need.

Getting ready

For the purpose of this recipe, let's focus on reusing the previous solution. We will check the value of a field, and act upon it.

How to do it...

In order to walkthrough this recipe, follow these steps:

  1. Create a new form field called new_changeevent, with a label of Change Event, and a Type of Two Options. Leave the default values of No and Yes. Leave the Default Value as No.
    How to do it...
  2. Add this field to your main Contact form.
  3. Add the following script to a new JScript web resource:
    function ChangeEvent()
    {
      var _changeEventSelection = null;
      var _isChanged = Xrm.Page.getAttribute("new_changeevent");
      
      if(_isChanged != null)
      {
        _changeEventSelection = _isChanged.getValue();
      }
      
      if(_changeEventSelection == true)
      {
        alert("Change event is set to True");
        // perform other actions here
      }
      else
      {
        alert("Change event is set to False");
      }
    }
  4. This function, as seen in the previous recipes, checks the value of the Two Options field, and performs and action based on the user selection. The action in this example is simply bringing an alert message up.
  5. Add the new web resource to the form libraries.
  6. Associate this new function to the OnChange event of the field we have just created.
  7. Save and Publish your solution.
  8. Create a new contact, and try changing the Change Event value from No to Yes and back. Every time the selection is changed, a different message comes up in the alert.

How it works...

Handling events at the field level, specifically the OnSave event, allows us to dynamically execute various other functions. We can easily take advantage of this functionality to modify the form displayed to a user dynamically, based on a selection. Based on a field value, we can define areas or field on the form to be hidden and shown.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.144.18