Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

unable to retrieve text of selected option set

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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

  • matt_hirst Profile Picture
    matt_hirst 5 on at
    RE: unable to retrieve text of selected option set

    Hi,

    Yes I got around it by using the above code.

    Matt

  • Suggested answer
    Charles Abi Khirs Profile Picture
    Charles Abi Khirs 3,569 on at
    RE: unable to retrieve text of selected option set

    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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: unable to retrieve text of selected option set

    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
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: unable to retrieve text of selected option set

    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
    Community Member Microsoft Employee on at
    RE: unable to retrieve text of selected option set

    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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: unable to retrieve text of selected option set

    Goutam,

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

    regards,

    Matt

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: unable to retrieve text of selected option set

    Hi Matt,

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: unable to retrieve text of selected option set

    Also get an error with your code:

    Object doesn't support property or method 'forEach'

    Matt

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: unable to retrieve text of selected option set

    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

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: unable to retrieve text of selected option set

    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.

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans