Michael Philip's Blog

Writing about MS Tech with strong focus on Azure & CRM

MS CRM – Set Option Set by Text or Value Using Java Script

Leave a comment

Brief :

Consider having an option set on a Dynamics CRM 2011/2013/2015 form and you want
to change the selected item or it have no selected item currently and you would like to set it.

You have to 2 ways to do it.

  • By Value

    Xrm.Page.getAttribute("optionsetAttribute").setValue(value);

  • By Text

    Actually you can’t set the selected item by text by you can search all options untill you find the on whose text (label) is the same as one the one you have then get its value


    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);
    }

    }

     

    ExamplesetOptionSetByOptionText ("new_nationality","American");

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s