Replacing the Country and Province fields with lookups

Even though we touched on bits and pieces of this recipe earlier in the previous chapters, I will be presenting here a top-to-bottom approach of replacing the standard out-of-the-box Country and Province fields with lookups. This solution will allow users to select these values from pre-defined available options, and eliminate the issues resulting from typical user input errors.

Getting ready

For this recipe, use one of the solutions created in earlier chapters, or create a new one. Add to your solution the Account and Contact entities where we will replace the fields with lookups.

How to do it...

Follow the same steps on both the Account and the Contact entities. I will only describe the process once. You can follow the same process if you create a custom Address entity, or anywhere else where you need to capture address information.

The end result will be a transformation of the following field set:

How to do it...

To the following:

How to do it...
  1. Open your existing solution.
  2. Add a new entity named Country, with an internal name of new_country.
    How to do it...
  3. Make it visible only on the Settings area:
    How to do it...
  4. Save your new entity.
  5. Open the main form of the entity, and modify the form as follows:
    1. Add a field named Country Code, with an internal name of new_countrycode. Format it as text, with a maximum length of 3. This field will hold an abbreviated version such as USA, CA, and UK.
    2. Add a field named Display Sequence, with an internal name of new_displaysequence. Format it as a whole number, with a minimum value of 0. I have set the maximum to 500, but you could set it to the total number of countries, which at the time of this writing stands at 196.
    3. Add these fields to your form for an end result as follows:
    How to do it...
  6. Add a new entity named State, with an internal name of new_state.
    How to do it...
  7. Make it visible only on the Settings area.
    How to do it...
  8. Save your new entity.
  9. Open the main form of the entity, and customize it as follows:
    1. Add a new field named State Code, with an internal name of new_statecode. Format it as text, with a maximum length of 5, or enough to capture a short name for the state. Usually for US and Canada two characters are enough.
    2. Add a new field name Display Sequence, with an internal name of new_displaysequence. Format it as a whole number, with a minimum value of 0. Set the maximum depending on the number of provinces/states you intend to capture in the system. If unsure, set it to a value high enough for your initial phase. You can always change the maximum value at a later time.
    3. Add a new field named Country, with an internal name of new_country. Format it as type lookup, with a target record type pointing back to the Country entity we have just created.
      How to do it...
    4. Add these new fields to your form, for an end result as follows:
    How to do it...
  10. Save your new entity.
  11. Publish all changes to this solution.
  12. Open your Account entity or the entity where we will replace the standard text fields with our new lookups. Open the main form for editing.
  13. Add a new field named State, with an internal name of new_state. Make the field type lookup, with a target record type of State (the entity we created earlier). Add this field to our form right underneath our existing State/Province field.
    How to do it...
  14. Similarly, add a new field named Country, with an internal name of new_country. Make the field type lookup, with a target record type of Country (the entity we created earlier). Add this field to our form.
  15. Double-click the State field we just added, and on the Display tab, in the Related Records Filtering area, check the box next to Only show records where:.
  16. Configure as in the following image:
    How to do it...
  17. Turn off the check box on Allow users to turn off filter, so that users will always get a listing of the provinces related to the country selected.
  18. In order to make the input work, move the country selection above the State. This allows the users to select a country first, and then get a listing of all state/provinces related to the country they already selected.
  19. Make the default fields for State/Province and Country/Region not visible, by opening the field properties window, and unchecking the Visible by default checkbox.
    How to do it...
  20. I have arranged the fields on the form as follows:
    How to do it...
  21. Save and Publish your entire solution.
  22. Refresh your browser window to see the two new entities we have just created. Navigate to the Settings section. You should see them listed as follows in the Extensions area.
    How to do it...
  23. Open first the Countries, and add a few sample records.
  24. Open next the States and add a few states/provinces for each of the added countries.
    How to do it...
  25. Once you have some sample data, check the Account form to make sure the filtering indeed takes place.

    Tip

    The Display Sequence field can be used to order the views.

  26. In order to keep the out-of-the-box functionality in place, and to allow some of the standard reports and views to function as expected, we will add two functions that will help us copy the values selected by the user in the lookup fields to the standard text fields that we have just marked as hidden.
  27. Add a JScript web resource, and insert the following two functions:
    function UpdateCountry()
    {
      var _country = new Array();
      _country = Xrm.Page.getAttribute("new_country").getValue();
      if(_country != null)
      {
       Xrm.Page.getAttribute("address1_country").setValue(_country[0].name);
      }
    }
    
    function UpdateState()
    {
      var _state = new Array();
      _state = Xrm.Page.getAttribute("new_state").getValue();
      if(_state != null)
      {
        Xrm.Page.getAttribute("address1_stateorprovince").setValue(_state[0].name);
      }
    }
  28. Associate the UpdateCountry() function with the OnChange event of the new country lookup we created, and the UpdateState() function to the OnChange event of the new State lookup.
  29. Update a few records, and check using Advanced Find to make sure that the original fields are being also populated.

How it works...

The process I am following in this recipe is relatively simple. We are creating the related lookups, and populating the selected values into the original fields using the JavaScript functions. We could have done this without copying the values into the original fields, but the reason I chose to do so is because now I can use all the views and reports as defined out of the box, and any future upgrades or other solutions that are referencing those fields will find them also populated with the current values.

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

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