Tip #524: Inactive optionsets

One of the advantages of using lookups vs optionsets is the ability to create a “dynamic” set where the available values can be filtered and deactivate when they become obsolete. Optionsets still are very useful because they are easy to set up and use – dropdown as a UI element is all too familiar to discard.

So what can you do if you need to remove some of the values from the existing optionset? Firstly, using advanced find, figure out if the value to be removed is actually used. If not, simply remove it from the optionset. If the value is in use, however, and you don’t want to break existing functionality, then values can be filtered on the fly by removing them when the form loads.

function form_onload() {
   var value = Xrm.Page.getAttribute("industrycode")
                       .getValue();
   var ctrl = Xrm.Page.getControl("industrycode");
   var inactive = getInactiveOptions("industrycode");
   inactive.forEach(
      function(i) {
         // check if value is in use
         if(i != value) ctrl.removeOption(i);
      }
   );
}

function getInactiveOptions(field) {
   if(field == "industrycode") 
      return [1,2];
   else
      return [];
}

This script removes couple values from the industry code list when the form loads. It does not completely solve the issue but it does the job as far as user interface is concerned. You can improve usability of the script by adding words “(obsolete)” to the inactive values that are “in use”. More advanced versions could use a custom action call to the server to find out the list of inactive values – that way they can be maintained in one central location (helper entity, for example).

Share on FacebookTweet about this on TwitterShare on Google+

Leave a Reply

Your email address will not be published. Required fields are marked *