locked
CRM OptionSet Ranking RRS feed

  • Question

  • I've been trying to find a more elegant way of writing this JavaScript code instead of a bunch of IF statements. I want to write a ranking function where once you select one option, that option is not available in the other fields. And if you change your selection, the previous value needs to be added back into the list so it can be selected again.

    For example, if I have four option set fields called Coke, Sprite, Root Beer and Ginger Ale with all having the same values 1,2,3,4.

    I want the user to be able to rank what Soda they like with 1 being the highest and 4 being the lowest. If the user select 1 for Coke, they cannot select 1 for any of the other fields.

    Can anyone help with this?


    Mike Karls

    Wednesday, September 24, 2014 3:30 PM

All replies

  • I think you can find an example of what you are looking for here.
    Thursday, September 25, 2014 8:17 PM
  • I don't believe that code performs the ranking functionality I am looking for by removing values when they are selected and, just as important, adding the value that was previously selected if the selection changes.


    Mike Karls

    Thursday, September 25, 2014 9:03 PM
  • Hello,

    You can add remove option set options based on their index, check this


    Microsoft Dynamics CRM Training|Our Blog | Follow US | Our Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Friday, September 26, 2014 2:49 AM
    Moderator
  • Coding this task is a bit awkward for two reasons :

    First, CRM doesn’t have a field.LastValue concept, so you need to retain/manage the last value for each option set when the user deselects / selects options.

    Second, options are appended to the end of the list unless you intend to manage removed/added indexes yourself.

    A better way, which is more CRM “fluent” is to progress the values depending on user selection.

    i.e. if the user already selected 1 for soda and then selects 1 for coke then the code progresses soda to 2 and so forth.

    The simplest implementation requires the following code (note: this is just pseudo I wrote into this thread)

    var softDrinks = [];
    
    function OnCrmLoad() {
        OnSoftDrinkInit();
    }
    
    function OnSoftDrinkInit() {
        softDrinks[0] = getOptionSet("new_sprite");
        softDrinks[1] = getOptionSet("new_coke");
        softDrinks[2] = getOptionSet("new_rootbeer");
        softDrinks[3] = getOptionSet("new_gingerale");
        
        for(var i = 0 ; i < softDrinks.length ; i++) {
            softDrinks[i].Attribute.addOnChange(OnSoftDrinkChange)
        }
    }
    
    function OnSoftDrinkChange(eContext) {
        var source = eContext.getEventArgs().getSource(),
            currDrink = null,
            currDrinkValue = null;
        
        for(var i = 0 ; i < softDrinks.length ; i++) {
            if (source.getId() != softDrinks[i].Id) {
                currDrink = softDrinks[i].Attribute;
                currDrinkValue = currDrink.getValue();
                currDrinkValue == softDrinks.length 
                    ? currDrink.setValue(null)
                    : currDrink.setValue(currDrinkValue + 1);
            }    
        }
    }
    
    function getOptionSet(id) {
        var ctl = Xrm.Page.getAttribute(id);
        var atr = Xrm.Page.getControl(id);
        
        return {
            Contorl: ctl,
            Attribute : atr,
            id : ctl.getId()
        }
    }


    Dev Blog: Dynamics CRM - Thinking outside the Box

    Friday, September 26, 2014 11:27 AM