none
CRM 2011: Adding option to optionset using javascript

    Question

  • Hi all,

    Could anybody please help me adding option to an optionset using javascript

    I tried the below:

    					var advanceOption = new Option();
    					advanceOption.value = 2;
    					advanceOption.text = "Advance";
    
    					Xrm.Page.getControl( "optionsetfieldname" ).addOption( advanceOption, 3 );
    My understanding is that addOption function takes two parameters, the new option and the option order in optionset

    Is that true ?



    Thanks in advance..
    Sunday, December 09, 2012 12:49 PM

Answers

  • As Scott wrote, you have to have those options in the metadata first - you cannot just add any option to the control. Let's say you have an attribute, which is an option set with 3 possible options. "addOption" will only recognize options from those 3 - you can't add an absolutely new option (which is outside of the possible values for this attribute).

    It does not have anything to do with the "onload" - it has to do with the metadata. 

    Most likely, what you really need is a "reverse" approach - when defining an attribute, add all possible options. In the "onload", use removeOption to remove some options from the cotnrol.


    Alex Shlega, GotchaHunter.Net

    Monday, December 10, 2012 3:08 AM

All replies

  • Any suggestions ?
    Sunday, December 09, 2012 8:21 PM
  • Hi, You are right! See http://msdn.microsoft.com/en-us/library/51828fe3-f6ff-4f97-80ed-b06b3a354955#BKMK_addOption

    Are you getting an error? If so, can you post your code and the error?

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public     Follow Me on Twitter
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Sunday, December 09, 2012 8:46 PM
    Answerer
  • I tried  the below:

    var optionsetField = Xrm.Page.getAttribute( "optionsetfield" );
    var Options = optionsetField.getOptions();
    
    var advanceOption = new Option();
    				advanceOption.value = 2;
    				advanceOption.text = "Advance";
    
    				Xrm.Page.getControl("optionsetfield").addOption(advanceOption, 3);
    
    
    for (i = 0; i < Options.length; i++)
    				{
    				    alert(optionsetField.getOptions()[i].value + " " + optionsetField.getOptions()[i].text);
    				}

    And in the third option which is the new option it displays null

    Please advise



    Sunday, December 09, 2012 9:30 PM
  • Hi, Do you mean it displays null after you have saved it? You will need to make sure the optionset metadata contains a valid entry for value 2.

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public     Follow Me on Twitter
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Sunday, December 09, 2012 10:31 PM
    Answerer
  • This code is executed in onload event, should I save the form before trying to use the new value ?
    Sunday, December 09, 2012 10:49 PM
  • As Scott wrote, you have to have those options in the metadata first - you cannot just add any option to the control. Let's say you have an attribute, which is an option set with 3 possible options. "addOption" will only recognize options from those 3 - you can't add an absolutely new option (which is outside of the possible values for this attribute).

    It does not have anything to do with the "onload" - it has to do with the metadata. 

    Most likely, what you really need is a "reverse" approach - when defining an attribute, add all possible options. In the "onload", use removeOption to remove some options from the cotnrol.


    Alex Shlega, GotchaHunter.Net

    Monday, December 10, 2012 3:08 AM
  • That makes sense, but what if I need to add/remove the option based on another field value ?
    Monday, December 10, 2012 9:49 AM
  • As an alternative(if you really don't know the list of possible options in advance to add them to the metadata), you could create your own drop down using either a web resource or direct HTML manipulations on the CRM form.. You might use a 'whole number' crm field to store the value selected in that combobox. You can't really add such combo box to the advanced find, though, so there will be limitations as well.

    Alex Shlega, GotchaHunter.Net

    Monday, December 10, 2012 12:59 PM
  • As Scott wrote, you have to have those options in the metadata first - you cannot just add any option to the control. Let's say you have an attribute, which is an option set with 3 possible options. "addOption" will only recognize options from those 3 - you can't add an absolutely new option (which is outside of the possible values for this attribute).

    It does not have anything to do with the "onload" - it has to do with the metadata. 

    Most likely, what you really need is a "reverse" approach - when defining an attribute, add all possible options. In the "onload", use removeOption to remove some options from the cotnrol.


    Alex Shlega, GotchaHunter.Net

    ...okay, does this mean, that it is not possible to add some new "unknown" options to an existing (empty by customization) option set/picklist?

    Where is the sense? Actually, exactly that is what a dynamic picklist is supposed to be, "dynamic"!

    Could somebody commit this? - Theres no supported way to populate a picklist with customization-unknown elements???

    Thx, for your help!

    Berney

    Thursday, March 06, 2014 4:22 PM
  • You just have to keep in mind how it's implemented in CRM. A pick list is not just a control that you can populate in whichever way you want - it's a combination of predefined option values and "dropdown behavior". First, you define which options are going to be available for that control (in the customizations aread). then you can work with them using Xrm javascripts: show some of them, hide others.. but they all must belong to the same pre-configured list. 

    This is also tied to the validation. Technically, you can use javascript to add an option that has not been preconfigured. However, CRM will show you an error if you try to save that selection to the database.


    Alex Shlega, GotchaHunter.Net


    • Edited by Alex Shlega Thursday, March 06, 2014 4:35 PM
    Thursday, March 06, 2014 4:32 PM
  • Hi Alex,

    thanks for your reply. Other question:

    I do understand, that it is not possible to populate a pick list and store/save the selected option. What I want to do is, using a picklist (empty by customization), populate it onLoad or onChange Event, read the selected Option (onChange) and wite/store a GUID or any other value (related to the selected option) in a free text-field on the form.

    Is there any supported way to do this...??

    Thank you in advance for any help!

    Berney

    Friday, March 07, 2014 9:04 AM
  • Hi Berney,

      at the minimum, you would likely have to write your own OnLoad and OnSave javascript handlers to populate that picklist in the OnLoad and to clean it up in the OnSave (+, of cause, OnChange to push picklist selection to your textbox). With that said, Microsoft may choose to update that API to do additional validations on the client, so such a script may start failing if/when it happens.

      Have you tried using a web resource instead? You might create a web resource, add a drop down to it, then put it on the form, and just do all those things using a web resource. You'll probably have to play with CSS styling a little to make it blend with Dynamics form, but that should be possible, too.

      One caveat I see there is that, whatever approach you choose, you won't be able to use that dropdown in any of the reports (you can't do those "dynamic population" tricks in the advanced.. you may still be able to do it if you build custom SSRS reports, but it's not what regular users would be able to do).

       BTW, I wonder if a lookup would work better. You might use addCustomView function with the lookups to define the view(to display only a subset of entities, for example), and that would be totally fine for Dynamics. 


    Alex Shlega, GotchaHunter.Net

    Friday, March 07, 2014 1:53 PM
  • Hi Alex,

    I' m not sure, if I understood right, or if you got my problem in detail:

    What I do is, get all products (ODATA) (of eg. related quote) and populate a picklist.

    With the picklist the user should be able to point the current data at the quote-detail (product). OnChange I want to read the selected option of the picklist and save it (quote.detail.productid) in a textfield.

    But reading the selected option fails!

    some snippets:

    function retrieveEntityData(entity)
    {
    	if (entity.results.length >0)
    	{
    		// define Picklist Control on Form
    		var picklistControl = Xrm.Page.getControl("es_orderpos");
    		picklistControl.clearOptions();
    		var order = entity.results[0]
    		if (order != null)
    		{
    			var orderPos = order.order_details;
    			var posCount = orderPos.results.length;
    			var pos = 0;
    			var role = new Array();
    			var posId = new Array();
    			while (pos < posCount)
    				{
    						if (orderPos.results[pos].ProductId.Name !== null)
    						{
    							var pickOption = new Option();
    							pickOption.text = orderPos.results[pos].ProductId.Name;
    							pickOption.value = orderPos.results[pos].ProductId.Id;
    						}
    						
    					
    					picklistControl.addOption(pickOption);
    					pos = pos+1;
    				}
    				if (posCount == 0)
    				{
    					var pickOption = new Option();
    					pickOption.text = "no Products";
    					pickOption.value = "0";
    					picklistControl.addOption(pickOption);
    				}
    			
    		}
    	}
    }


    function setGuid()
    {
    	var picklist = Xrm.Page.getAttribute("es_orderpos");
    	var selection = picklist.getSelectedOption();
    	alert (selection);
    }

    may this help to understand!?

    Thx

    Berney

    Monday, March 10, 2014 4:53 PM
  • Hi Berney,

      this is where it gets out of sync between attribute and control objects.. You can add an option to the control and it shows up in the user interface, but, as it seems, it does not get added to the attribute, so it can't be "selected" on the attribute level. Again, it is not expected to work like that, so we are really talking about non-supported way of doing things. Supported way of doing this might be to add a web resource where you would put a combo box(and add scripting to populate it)

      What you might probably try in your onchange, though, you might try to get access to the underlying HTML element and read the selection from there (just use getElementById with your control name, and, for the rest of it, you can check this page, for instance: http://www.mredkj.com/tutorials/tutorial002.html)



    Alex Shlega, GotchaHunter.Net

    Tuesday, March 11, 2014 4:07 PM