madbrendon madbrendon - 2 months ago 7
Javascript Question

Dynamically Change Option Set Values in CRM

I am using CRM Online 2013.

I am trying to remove 3 values from an optionset under a certain condition.
The optionset has six options by default: they are listed at the top of my JS code below.

When I run my code, the correct amount of options appear; but they all say undefined.

Undefined Image example

Here is what I have at the moment:

var customer = 100000000;
var partner = 100000001;
var partnerCustomer = 100000002;
var customerAndBeta = 100000003;
var partnerAndBeta = 100000004;
var partnerCustomerAndBeta = 100000005;

function populateBetaOptionSet(beta) {
var options = Xrm.Page.getAttribute("intip_websiteaccess").getOptions();
var pickListField = Xrm.Page.getControl("intip_websiteaccess");

for(i = 0; i < options.length; i++)
{
pickListField.removeOption(options[i].value);
}

if (beta == false) {
pickListField.addOption(customer);
pickListField.addOption(partner);
pickListField.addOption(partnerCustomer);
}

pickListField.addOption(customerAndBeta);
pickListField.addOption(partnerAndBeta);
pickListField.addOption(partnerCustomerAndBeta);
}


This is being called from another function which is wired up to a separate field's onchange event. I am sure this is working correctly as I am getting the correct beta value through when it is called.

I am removing all the options before re-adding them to avoid duplicates.

Any idea what I am doing wrong here/or know of a better way of doing this?

Answer

Re-wrote your function to match the criterion. The option is an object with both text and value. This is why you see undefined (missing text); So instead of

var customer = 100000000 

it needs to be

var customer = { value : 100000000 , text : "Customer" };

The code below saves each option in global scope and uses it each time you call populateBetaOptionSet

function populateBetaOptionSet(beta) {
    var xrmPage = Xrm.Page;
    var pickListField = xrmPage.getControl("intip_websiteaccess");
    var options = pickListField.getOptions();

    //save all options  
    if (!window.wsOptions)
    {
         window.wsOptions = {};
         wsOptions.customer = pickListField.getOption(100000000);
         wsOptions.partner = pickListField.getOption(100000001);
         wsOptions.partnerCustomer = pickListField.getOption(100000002);
         wsOptions.customerAndBeta = pickListField.getOption(100000003);
         wsOptions.partnerAndBeta = pickListField.getOption(100000004);
         wsOptions.partnerCustomerAndBeta = pickListField.getOption(100000005);
    }

    //clear all items
    for(var i = 0; i < options.length; i++)
    {
            pickListField.removeOption(options[i].value);           
    }

    if (beta == false) {
        pickListField.addOption(wsOptions.customer);
        pickListField.addOption(wsOptions.partner);
        pickListField.addOption(wsOptions.partnerCustomer);
    }

    pickListField.addOption(wsOptions.customerAndBeta);
    pickListField.addOption(wsOptions.partnerAndBeta);
    pickListField.addOption(wsOptions.partnerCustomerAndBeta);
}