Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Display a <select> by retrieving option set in a web resource

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi,

I'm new on Dynamics 365, and I still don't succeed in retrieving my option set text/values.

I would like display the options by using a web resource searching in an option set the options available. 

But after some work, I still don't succeed. 


<html><body>

<select multiple id="selectForm">
</select>
</body></html>

<script>
var attribute = Xrm.Page.getAttribute('new_optionset_prenom'); var optiontable = attribute.getOptions(); var i = 0; for (i in optiontable)
{ document.getElementById("selectForm").options[document.getElementById("selectForm").options.length] =
new Option(optiontable[i], i);

}
</script>

I imagined that optiontable was a table of object for every option, but I don't succeed in manipulate it.

In the end I would like to get an exemple like that : 

azertyuiop.PNG

because my option set has some name inside.

thank you for your help !

regards

*This post is locked for comments

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Display a <select> by retrieving option set in a web resource

    Hi,

    You try writing a javascript function in the script and call this function on the load of the body of the HTML web resource as shown below.

    <script>
     function onBodyLoad(){
       debugger;
       var optionsetLabel = vals[0];
       console.log('optionSet name :', optionsetLabel);
       //champ CRM
       var fieldcrm = vals[1];
       console.log('field of the crm :', fieldcrm);	
    
       var attribute = window.parent.Xrm.Page.getAttribute(optionsetLabel);
       var optiontable = attribute.getOptions();
       var dropdown = document.getElementById("selectForm");
    
       var i = 0;
       for (i in optiontable) {
          var text = optiontable[i].text;
          var value = optiontable[i].value;
          var option = document.createElement("option");
          option.text = text;
          option.value=value; // or you can set i instead if you want
          dropdown.add(option);
       }
     }
    </script>
    
    <body onload = "onBodyLoad()" >
        <select multiple id="selectForm">
        </select>
    </body>

    You could now try debugging the code using developer tools.

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Display a <select> by retrieving option set in a web resource

    Thank your for your help !

    Unfortunately, didn't succeed in retrieving the options.

    I really don't know what is the problem. I'm using a web resource html and the "console.log(arg); " even doesn't appear in the console part of F12. I think this is because of the web resource html. So the browser doesn't access to those variable.

    here is my code in the web resource html : 

    <script>
       var optionsetLabel = vals[0];
       console.log('optionSet name :', optionsetLabel);
       //champ CRM
       var fieldcrm = vals[1];
       console.log('field of the crm :', fieldcrm);	
    
       var attribute = window.parent.Xrm.Page.getAttribute(optionsetLabel);
       var optiontable = attribute.getOptions();
       var dropdown = document.getElementById("selectForm");
    
       var i = 0;
       for (i in optiontable) {
          var text = optiontable[i].text;
          var value = optiontable[i].value;
          var option = document.createElement("option");
          option.text = text;
          option.value=value; // or you can set i instead if you want
          dropdown.add(option);
       }
    </script>
    
    <body>
        <select multiple id="selectForm">
        </select>
    </body>
    


  • Verified answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Display a <select> by retrieving option set in a web resource

    Hi,

    If you are using this javascript inside an HTML web resource, it should be as follows.

    var attribute = window.parent.Xrm.Page.getAttribute('new_optionset_prenom');

    Hope this helps.

  • Verified answer
    SCV Profile Picture
    SCV 1,004 on at
    RE: Display a <select> by retrieving option set in a web resource

    Hi,

    Since you are inside a web resource you will have to use the word parent like below: 

    var attribute = parent.Xrm.Page.getAttribute('new_optionset_prenom');

      

    Then modify the code like below:

     

    var attribute = Xrm.Page.getAttribute('new_customerseverity');
    
    var optiontable = attribute.getOptions();
    
    var dropdown = document.getElementById("selectForm");
    
    for (var i = 0; i < optiontable.length; i++) {
    
         var text = optiontable[i].text;
    
         var value = optiontable[i].value;
    
     
    
         var option = document.createElement("option");
    
         option.text = text;
    
         option.value=value; // or you can set i instead if you want
    
         dropdown.add(option);
    
    }
    

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 Pt 2

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,514 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans