locked
How to set OptionSet Values in Javascript CRM 2011 RRS feed

  • Question

  • Hi Techies,

    Could any one please give me the code snippet to set Option Set value using JavaScript.

    I am using the below Code, But it is not setting value.

    Xrm.Page.getAttribute(“new_country”).setValue(‘United States’);

    Please advise.

    Thank you,


    Sreeni Pavalla

    Wednesday, April 18, 2012 7:06 AM

Answers

  • Yes it is possible you need to iterate your Optionset in JavaScript.
    like this
    function SetValue(optionsetAttribute, optionText)
    {
    	var options = Xrm.Page.getAttribute(optionsetAttribute).getOptions();
    	for(i = 0; i < options.length; i++)
    	{
    		if (options[i].text == optionText)
    			Xrm.Page.getAttribute(optionsetAttribute).setValue(options[i].value);
    	}
    }
    Calling of the function
    SetValue("new_country", "Hyderabad")

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!

    Wednesday, April 18, 2012 9:11 AM

All replies

  • function onLoad()
    {
    Xrm.Page.getAttribute(“new_country”).setValue(1);
    }

    CRM 2011 does not accept label you need to set Values for this purpose.
    For example you United States is the label on option set and its value is 1 than you need to setValue of 1

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!



    Wednesday, April 18, 2012 7:59 AM
  • You cannot set the text value of the option or sting value. You have to set the integer value for your optionset.

    Xrm.Page.getAttribute("new_country").setValue(1);

    Setting the integer value is recommended because the text value of the option might be changed. But if you just want to set the value with the text value according to your business requirement, you can use the following code for it.

    setOptionSetByOptionText("new_country", "United States");
    
    
    setOptionSetByOptionText = function(optionsetAttribute, optionText)
    {
    	var options = Xrm.Page.getAttribute(optionsetAttribute).getOptions();
    	for(i = 0; i < options.length; i++)
    	{
    		if (options[i].text == optionText)
    			Xrm.Page.getAttribute(optionsetAttribute).setValue(options[i].value);
    	}
    }

    Wednesday, April 18, 2012 8:27 AM
  • Thanks for the Reply Mubasher.

    It will be bite difficult to select the optionsetvalue if i have 100 options in the Options.

    Is there anyway that i can get the Value of one label from Javascript and set back it to OptionSet.

    I have a optionset which has 5 options like below

    1. Hyderabad

    2. Bangalore

    3. Chennai

    4. Mumbai

    5. Delhi

    As per your reply, If need my optionset to select Bangalore, I should have the value 2. So that i can set.

    But as i cannot maintain Values and labels on the same javascript, Is there any way that i can get the Value by passing the Label.

    Once i get the value, That will be easy for me to set the value right?

    In this case,Could you please suggest me how to get the Value by passing label.

    Thank you,


    Sreeni Pavalla

    Wednesday, April 18, 2012 8:31 AM
  • Yes it is possible you need to iterate your Optionset in JavaScript.
    like this
    function SetValue(optionsetAttribute, optionText)
    {
    	var options = Xrm.Page.getAttribute(optionsetAttribute).getOptions();
    	for(i = 0; i < options.length; i++)
    	{
    		if (options[i].text == optionText)
    			Xrm.Page.getAttribute(optionsetAttribute).setValue(options[i].value);
    	}
    }
    Calling of the function
    SetValue("new_country", "Hyderabad")

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!

    Wednesday, April 18, 2012 9:11 AM
  • Thursday, April 19, 2012 9:27 AM
  • Thanks buddy its really a great help

    Friday, December 7, 2012 9:34 AM
  • Thanks buddy its really of great help.........
    Friday, December 7, 2012 9:35 AM
  • hi, try this

    Xrm.Page.data.entity.attributes.get("statuscode").setValue(100000000);

    Friday, June 9, 2017 11:44 AM
  • Hi Please try these...

    Xrm.Page.getAttribute("vtv_status").setValue(100000000);

    Monday, June 12, 2017 5:12 AM