0
votes

I am upgrading a CRM 4 solution to CRM 2011 and I am having an issue with a dynamic Option Set.

I am populating the option set by calling an API from the Javascript. This returns a list of text and value options, which I then iterate through.

Firstly, I retrieve the object itself and add a static Default option to the list:

var varPicklist = Xrm.Page.getControl("list_name");

//Add a System Default option to the picklist
var opt1 = new Option();
opt1.text = "System Default";
opt1.value = 100000;
varPicklist.addOption(opt1);

I then iterate through the collection, create an object and add each one to the list:

//Populate the rest of the list
var ObjectList = xmlDoc.getElementsByTagName("LookupItem");
for (var i = 0 ; i < ObjectList.length ; i++) 
{
    var optValue
    var optText
    var ObjectListItems = ObjectList[i].childNodes;
    for (var j = 0 ; j < ObjectListItems.length ; j++)
    {
        var value = ObjectListItems[j].childNodes[0];
        if (ObjectListItems[j].nodeName == "Key")
        {
            optValue = value.nodeValue;
        }
        else if (ObjectListItems[j].nodeName == "Value")
        {
            optText = value.nodeValue;
        }
    }

var opt = new Option();
opt.text = optText;
opt.value = optValue;
varPicklist.addOption(opt);
};

As stated, this all seems to work. The Picklist gets populated with the values and doesn't error. If I stick an alert in there during this process, the correct values or labels are displayed.

However, somehow the items seem to get lost along the way. If I try to reference them in any way from elsewhere in the form, I get null back. I tried this;

Xrm.Page.getAttribute("list_name").getSelectedOption().value

...and this;

Xrm.Page.getAttribute("list_name").getValue();

Neither of which worked. I tried accessing the text as opposed to the values (just as a test) and couldn't get those either.

But when I tried the same methods with option sets which contained static options, I could access them no problem.

Any help with where I'm going wrong would be appreciated...

1
What I understand from your question is, you already have stored all the optionset list in a variable called "varPicklist". right ? Than what you want to do ? is not clear enoughmzh
No, what I'm doing is I start with an empty Option Set control on the form. Then on Form Load, the Javascript calls an API. This returns an XML object, which contains all of the options. I then loop through the options, create an Option object, and add them to the list, one at a time. Then later on in the form, I try to reference the options in the list. The problem is, I am unable to reference these options at this point.odinel

1 Answers

2
votes

CRM picklist is a weird control. Though getAttribute and getControl are CRM supported DOM manipulator, not 100% compatible like you expect in dynamic options.

Until unless you have physical static options added in CRM picklist control customizations, you cannot use that by getAttribute.

Using getControl, only you are displaying your options, but its not original CRM picklist options to store in DB.

Add all the options as static, hide/show based on your need.

Update:

To add a new optionset value on the fly, use InsertOptionValueRequest to target the metadata service & execute the request. Read MSDN