locked
CRM 2011 Manually Adding items to OptionSet, then retreiving values RRS feed

  • Question

  • Hi there,

    I am writing my first bit of Javascript to manually populate an Optionset field with values. I then (through the OnChange event) interrogate the value of the item, and return an alert box to show me the value.

    The probkem is , any value I manually add returns null as a value. Any value I PREVIOUSLY hard coded into the optionset, via the field editor window with values returns value correctly.

    So my question is how do I properly add Items into an Optionset programmatically to return the right value in an on chnage event?

    Here is the code I have:

     

    if (typeof (IOS) == "undefined")
    { IOS= {}; }
    
    IOS.CaseLibrary = 
    {
    
    init : function(initialField)
    { 
    alert("Init!");
    var InitialField= Xrm.Page.data.entity.attributes.get(initialField);
    var controls = InitialField.controls.get();
    
    var optn = document.createElement("OPTION");
    optn.text = "Option 1";
    optn.value = 100000051;
    
    controls[0].addOption(optn,1);
    
    var optn1 = document.createElement("OPTION");
    optn1.text = "Option 2";
    optn1.value = 100000061;
    controls[0].addOption(optn1,2);
    }, //End init
    
    changeoption : function(context)
    {
    var attribute = context.getEventSource();
    alert(attribute.getValue());
    } //End changeoption;
    };
    

     

    "Option 1" and "Option 2" both get added to the OptionSet, and are selectable, but return an alert of null when I change the drop down.

    If I add two other items to the OptionSet via the field editor those values show correctly.

    How come?

     

    I am using CRM Online 2011. Init Function is called in the Onload Form Event area. ChangeOption is attached to the OptionSet OnChange function.

     

    Thanks.



    Wednesday, March 30, 2011 10:45 PM

Answers

  • I know that this is an older post, but for anyone who encounters it in the future be advised that this is not the correct approach.

    You should not use JQuery to interact with the Xrm.Page object model. By using JQuery you are going directly to the DOM, which is just another way to call document.getElementById.

    Please look at the documentation for Use the Xrm.Page Object Model and the reference documentation at Xrm.Page Reference for information and sample code. 

    The documentation about the control calls out:

    For most script development work outside of Microsoft Dynamics CRM, developers may be accustomed to referring to page elements by using the document.getElementById method. For Microsoft Dynamics CRM form scripts this method is not supported. It is important to recognize that the attribute stores the data and the control is simply the presentation of the attribute in the form. For controls bound to attributes you may need to adjust the way you are accustomed to accessing data in the form.

    If you have comments or questions about the documentation, please send them to Send comments about this topic to Microsoft (the link in the SDK document pages)

     


    Jim Daly Technical Writer Microsoft Dynamics CRM
    Tuesday, May 24, 2011 9:31 PM
    Answerer

All replies

  • Hello,

    Try using the Option class instead. Something like this:

    if (typeof (IOS) == "undefined")
    { IOS= {}; }
    
    IOS.CaseLibrary = 
    {
    
    init : function(initialField)
    { 
    alert("Init!");
    var InitialField= Xrm.Page.data.entity.attributes.get(initialField);
    var controls = InitialField.controls.get();
    
    var optn = new Option();
    optn.text = "Option 1";
    optn.value = 100000051;
    
    controls[0].addOption(optn,1);
    
    var optn1 = new Option();
    optn1.text = "Option 2";
    optn1.value = 100000061;
    controls[0].addOption(optn1,2);
    }, //End init
    
    changeoption : function(context)
    {
    var attribute = context.getEventSource();
    alert(attribute.getValue());
    } //End changeoption;
    };
     
    


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Biz-Forward.comCroitoriu.NET

    Saturday, April 2, 2011 2:50 PM
  • Hi,

     

    I changed the class to Option(); like you suggest but it's still returns a null. I'm really thinking it might be a bug now, unless the way I add it or reteive iit s wrong... I also could not get the SDK version of Dependant Option Sets to work either and I traced the issue to an If statement that queried the value of a selected options set that was also filled in by a dynamic add. That to returned a null value when it should have returned the correct items value...

     

    Very strange.

     

    Any ideas?

     

    Thanks.

    Saturday, April 2, 2011 9:45 PM
  • Moving to development to see if you can get some additional help

    Regards, Donna

    Thursday, April 7, 2011 9:03 PM
  • I have the same issue. Did you ever resolve this? 

     

    -David

    Tuesday, April 12, 2011 2:35 PM
  • So I ended up just using jquery for this and it works fine.

     

     var varMyValue = $("#new_optionset1 option:selected").text();

    Tuesday, April 12, 2011 6:06 PM
  • Nope that doesn't work for me...

     

    I entered :

     

    var varMyValue = $("#new_sample_category option:selected").text();              (new_sample_category is the name of my Field I'm changing)

     

    In my OnChange function above and I just get a Popup box saying "Error:Obejct Expected" when executing your line.

     

    Any ideas?

     

    Thanks.

     

    Tuesday, April 12, 2011 7:21 PM
  • Major,

    Did you add the "jquery1.4.1.min.js"?  You can download it at http://jquery.com/

    I did the following :

        //Because of the CRM 2011 Bug we use JQUERY
     //Getting The Value & Text
     var varMyValue = $("#new_mytestpicklist option:selected");
        if (varMyValue != null) {
            alert(varMyValue.text());
            alert(varMyValue.val());
     }
     
     //Setting The "SELECTED" option by Value => 100000001
      $("#new_mytestpicklist option[value='100000001']").attr("selected", "selected");

    The above code works like a charm for me :) Thx for the tip Bixter1!!!!

    Kind Regards,

    Frederic

     

    Friday, April 22, 2011 9:33 AM
  • Hi Frederic,

     

    Thanks for this. I've now got the file, but how do I intergrate this js file into my form or CRM 2011 to execute your code?

     

    Thanks.

    Friday, April 22, 2011 9:44 AM
  • Hi Frederic,

     

    Thanks for this. I've now got the file, but how do I intergrate this js file into my form or CRM 2011 to execute your code?

     

    Thanks.

    Major,

    add this file into your CRM as jscript web resources and and add this to the entity where you are trying to use it.

    for more info check here :

    http://gtcrm.wordpress.com/2011/02/15/creating-crm-records-from-jscript-in-2011using-the-rest-end-point/

    this will give you a fair idea on how to use the jquery in CRM 2011.



    vishal swami

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    http://msdynamics4you.blogspot.com
    Friday, April 22, 2011 10:33 AM
  • Major,

     

    Check this site for more information : http://ayazahmad.wordpress.com/2010/09/25/javascript-libraries-in-microsoft-dynamics-crm-2011/

     

    What I Did :

    1. Create a Webresource for the "JQUERY"-js file!

    2. Create a Webresource for you "Entity"-js File!

    3. Set the following in your “Entity”-js file :

    onMyPicklistChange = function() {

     //Getting The Value & Text
     var varMyValue = $("#new_mytestpicklist option:selected");
        if (varMyValue != null) {
            alert(varMyValue.text());
            alert(varMyValue.val());
     }
     
     //Setting The "SELECTED" option by Value => 100000001
      $("#new_mytestpicklist option[value='100000001']").attr("selected", "selected");

    }

    4. On the onchange of your picklist, enable the “Onchange” event and set “onMyPicklistChange();” as function!

    Try it out and it should work like a charm!

     

    Kind Regards,

    Frederic

    Friday, April 29, 2011 7:46 AM
  • Excellent I will try it out. Thanks very much. :)
    Friday, April 29, 2011 6:05 PM
  • I know that this is an older post, but for anyone who encounters it in the future be advised that this is not the correct approach.

    You should not use JQuery to interact with the Xrm.Page object model. By using JQuery you are going directly to the DOM, which is just another way to call document.getElementById.

    Please look at the documentation for Use the Xrm.Page Object Model and the reference documentation at Xrm.Page Reference for information and sample code. 

    The documentation about the control calls out:

    For most script development work outside of Microsoft Dynamics CRM, developers may be accustomed to referring to page elements by using the document.getElementById method. For Microsoft Dynamics CRM form scripts this method is not supported. It is important to recognize that the attribute stores the data and the control is simply the presentation of the attribute in the form. For controls bound to attributes you may need to adjust the way you are accustomed to accessing data in the form.

    If you have comments or questions about the documentation, please send them to Send comments about this topic to Microsoft (the link in the SDK document pages)

     


    Jim Daly Technical Writer Microsoft Dynamics CRM
    Tuesday, May 24, 2011 9:31 PM
    Answerer
  • Thanks Jim, we appreciate that information!

    Regards, Donna

    Wednesday, May 25, 2011 12:48 AM
  • Hi Jim,

    Until Microsoft provides developers with a "supported" mechanism for this sort of work, we will have to resort to using unsupported mechanisms.

    I had the same situation and resolved it as described above. My customer would not have been happy if I told them that I couldn't fulfil their requirements as Microsoft don't support it.

    Regards

    Chris Adams

     

    Saturday, July 16, 2011 1:52 PM
  • Chris -

    The key is that developers don't need to create new option objects in code. They should manipulate the option objects that already exist.

    All the valid options are already defined within the optionset attribute. Developers can retrieve them using the attribute.getOptions method.

    Developers can't add or remove the valid options for the attribute in script.

    However, developers can add or remove the valid options displayed for the control using the addOption, clearOptions, removeOption methods.

    Since the control is what the user sees, this is all a developer needs to be concerned about.

    Developers should avoid creating new options in script because they may not be valid. The options set in an optionset can be changed or removed. If their code isn't synchronized with the current valid values, developers can introduce bad data. By always beginning witht the collection of valid options, developers know they are always working with valid options. Does this make sense?

     

    Also -  developers shouldn't treat the Xrm.Page.ui control object like an HTML <select> control.

    The other suggestions in this post say to use JQuery, or an 'option class', or to use document.createElement. Each of these are treating the control like a standard HTML control, and it is not.

    This is why I recommend people read the conceptual documentation for Use the Xrm.Page Object Model and the reference documentation at Xrm.Page Reference for information and sample code.

     

    There is no use case where developers need JQuery or any DOM object document method to interact with the Xrm.Page object model.

    Of course developers can use JQuery, but the primary use cases are:

    1. As an option to use the REST endpoint (although technically JQuery isn't required for that)

    2. To interact with their own HTML Web resource DOM.

     

     


    Jim Daly Technical Writer Microsoft Dynamics CRM
    Monday, July 18, 2011 4:58 PM
    Answerer
  • Hi jim,

     

    The reason that I needed them was that the entity form I was using had an entity and attribute selector on it. If the attribute they selected was an option set I had to dynamically load the associated option set onto this entity. this was to create an entity to provide logic e.g if entity1,attribute1.value>x then do something, the logic entity allowed the user to add various entities and attributes to the logic. 

    Another situation where I dynamically loaded options was when an entity needed to hold a reference to a Dialogue. I don't believe there is a look up for dialogs, so had to load them into an option set.

    If you have better approaches to these please let me know, as it was hard work to do them :-)

    Regards

     

    Chris

     

    Thursday, July 21, 2011 2:45 PM
  • Chris -

    Can you tell me more about what you mean when you say "...the entity form I was using had an entity and attribute selector on it."? Does this mean you have some kind of custom control that provides a list of entities and the attributes for those entities? I can imagine some kind of set of dependent select controls but I'm not sure this is what you mean. Does this mean you are adding these as options to a CRM Optionset control?

    The other situation you mention is that you loaded dialog references into an option set.

    I"m guessing that you are adding a CRM Optionset to a form and then expecting to programmatically interact with it just as you would with an HTML select control that you would add to a page that you created. Keep in mind that a CRM Optionset control is bound to the attribute metadata and can only have the specific set of valid options defined by that metadata. It really isn't intended to be used in a different way.

    If I'm guessing correctly, The approach I would recommend is to create an HTML Web resource that contains your own HTML select controls (or a Silverlight tree view control) that presents the necessary UI for users to select the entity and attributes needed. Add that Web resource to the form and keep all your logic within that page. When the user makes their selection you can save their choices in a hidden Text field on the form so it can be saved with the record. The Xrm.Page objects are available from the parent of your Web resource.

    Basically I'm suggesting that it is better to create your own custom control embedded in a Web resource on a page. In that web resource page you can use JQuery and make it work exactly the way you want it and save any result within a hidden CRM attribute control on the page.

    I realize that this adds some tasks to get the CRM look and feel within your custom HTML Web Resource. I am working on getting better examples in the SDK for this. In the mean time, see the User Interface Style Guide http://msdn.microsoft.com/en-us/library/gg334496.aspx and note the guidance in Web Page (HTML) Web resources topic http://msdn.microsoft.com/en-us/library/gg309536.aspx about Display an HTML Web Resource in a Form with No Border.

    It sounds like you were able to make it work for your client. But for others who may read this post, please understand that the recommended strategy is to separate your custom UI requirements and not necessarily try to use OptionSet controls in a way they are not intended.

    Please forgive me if I've misunderstood and guessed incorrectly.

    Best regards,

     

     


    Jim Daly Technical Writer Microsoft Dynamics CRM
    Thursday, July 21, 2011 4:45 PM
    Answerer
  • Thanks for your insight, responding and sharing your knowledge Jim, always appreciated!

    Regards, Donna

    Thursday, July 21, 2011 5:44 PM
  • Use new Object() instead of new Option(). Then use addOption to add the object. Hope this might work.
    Wednesday, July 27, 2011 7:17 AM