web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

unable to retrieve text of selected option set

(0) ShareShare
ReportReport
Posted on by

I have a dropdown on my form, the contents are dynamicly populated when the form loads by:

function AddRecordsToOptionset(records)
{
    var optionsetField = Xrm.Page.ui.controls.get("a_textmoduletest2");

    for (var i = 0; i < records.length; i++)
    {
        optionsetField.addOption({ text: records[i].orb_name});
    }
}

Running the form I can see the optionset and dropdown retrieves the correct values and I can select one of them.

My problem is getting the selected option from the dropdown, it constantly returns null with everything I've tried:

var var_text = Xrm.Page.getAttribute("a_textmoduletest2").getText();

alert(var_text);

What is the correct way to get the text that the user has selected in Dynamics CRM 2011 via javascript?

regards,

Matt

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Matt, 

    Are you sure the element you are adding in the optionset are available in the optionset field. You should add both text and value .

    Try to write like below - 

    function OptionSetAddRemove() {
        var optionSet = Xrm.Page.ui.controls.get("new_myoptionset");
        var optionSetValues = optionSet.getAttribute().getOptions();
        optionSet.clearOptions();  // Clear All the options before add
        optionSetValues.forEach(function (element) { // Loop all the element of optionset and add element based on condition
            for (var i = 0; i < records.length; i++) {
                if (element.value == records[i].orb_value) // You need to compare text or value  and add the element in the optionset         
                    optionSet.addOption(element);              
    
            }
         });
    }

    Hope this helps.

  • Community Member Profile Picture
    on at

    Hi,

    Thanks for the response.

    I've updated it to add a value:

    function AddRecordsToOptionset(records)

    {

       var optionsetField = Xrm.Page.ui.controls.get("a_textmoduletest2");

       for (var i = 0; i < records.length; i++)

       {

           optionsetField.addOption({ text: records[i].orb_name, value: (i)});

       }

    }

    I still get no value or text.

    Matt

  • Community Member Profile Picture
    on at

    Also get an error with your code:

    Object doesn't support property or method 'forEach'

    Matt

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Matt,

    Try with the code which I shared , you just need to change on the red highlighted part.

  • Community Member Profile Picture
    on at

    Goutam,

    As I mentioned above with your code I receive an error Object doesn't support property or method 'forEach'

    regards,

    Matt

  • Community Member Profile Picture
    on at

    I've gone into the entity and manually created an option in the optionset, I then runcode to add an additional three options.  I have an onchange event for the field that should return the text and value of the selected item for the field.

    All four values appear in the drop down when clicked, selecting the item that was manually added the text and the value return them both as expected.  However if I select any of the three items added dynamically all that gets returned are blank values.

    What am I doing wrong with coded values that I add:

       var optionset = Xrm.Page.ui.controls.get("a_textmoduletest2");

       var options=new Object();

       options=[{value:1,text:"testoption1"},{value:12,text:"testoption2"},{value:13,text:"testoption3"}];

       optionset.addOption(options[0],1);

       optionset.addOption(options[1],2);

       optionset.addOption(options[2],3);

    regards,

    Matt

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Matt,

    Are you saying that you are adding some new options to the optionset? i.e. if the option has 1,2 & 3 and then in your code you are dynamically adding 4,5 which makes the total optiosn as 1,2,3,4,5?

    If the above is correct then I think I =t is expected that you are getting the selected value as null because that value is dynamically added and doen not exists.  I believe if you select the dynamics value and try to save the form, you will either get an error or the value will not be saved.

    In order to add options, the option needs to be added to the optionset.

    Hope this helps.

  • Community Member Profile Picture
    on at

    Hi Matt,

    Were you able to resolve this issue. I'm trying to do the same and not sure how to pull text of selected option set value.

    Any help would be appreciated.

    Thanks,

    Vijaya

  • Suggested answer
    Charles Abi Khirs Profile Picture
    3,569 on at

    Hello Matt,

    Did you try the below?

    Get the text of the selected option : Xrm.Page.getAttribute("fieldname").getText();

    Get the numeric value of the selected option : Xrm.Page.getAttribute("fieldname").getValue();

    If you are using the D365 V9, you must use the formContext instead of Xrm.Page.

  • matt_hirst Profile Picture
    5 on at

    Hi,

    Yes I got around it by using the above code.

    Matt

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans