Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Best way to display Description of Value in Option Set

Posted on by 45

Hello,

I have an Option Set and would like a tooltip or description / information bar to be displayed containing the description of the value the user has chosen.

What would be the best/recommended way to achieve this?

  • e0209 Profile Picture
    e0209 45 on at
    RE: Best way to display Description of Value in Option Set

    Thank you very much Clofly.

    Works like a charm!

  • Verified answer
    cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Best way to display Description of Value in Option Set

    Hi e0209,

    Yes, we can also get description of a selected option by retrieving metadata of a specific optionset field of a specific entity.

    e.g: The code below will retrieve description of the value of "Preferred Method of Contact" option set of Contact entity.(We still fire the function at the field onChange event.)

    function onChangeOfOptionSet(executionContext) {
    
        var formContext = executionContext.getFormContext();
        // Clear notification bar
        formContext.ui.clearFormNotification();
        // Set notification bar
        var option = formContext.getAttribute('preferredcontactmethodcode').getSelectedOption().text;
    
        var req = new XMLHttpRequest();
        req.open("GET", formContext.context.getClientUrl()   "/api/data/v9.1/EntityDefinitions(LogicalName='contact')/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?$select=LogicalName&$filter=LogicalName eq 'preferredcontactmethodcode'&$expand=OptionSet", true);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    var results = JSON.parse(this.response);
                    for (var i = 0; i < results.value[0].OptionSet.Options.length; i  ) {                   
                        var Label = results.value[0].OptionSet.Options[i].Label.UserLocalizedLabel.Label;
                        if (option === Label) {
                            formContext.ui.setFormNotification(results.value[0].OptionSet.Options[i].Description.UserLocalizedLabel.Label, 'INFO');
                        }
                    }
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();
        
    }

    Let's test code above:

    Description value of Any, Email and Phone

    pastedimage1591337951745v1.png

    pastedimage1591337967937v2.png

    pastedimage1591337984463v3.png

    Result:

    pastedimage1591338073114v4.png

    pastedimage1591338092053v5.png

    pastedimage1591338117062v6.png

    If no option is selected or if there is description for selected option, then notification bar won't appear.

    Regards,

    Clofly

  • e0209 Profile Picture
    e0209 45 on at
    RE: Best way to display Description of Value in Option Set

    Thanks Adrian,

    How do you propose that the end user see the description of the option set when selecting a certain option?

    It appears to me the description is only visible in the customization backend area.

    The values are provided by business so we can't change them and it will take quite a number of words to make it clear which will be too long for the field in practical terms.

  • e0209 Profile Picture
    e0209 45 on at
    RE: Best way to display Description of Value in Option Set

    Thanks Clofly, is there any way to pull the text in the notification from the Description of the Value in the Option set instead of hardcoding it into the script?

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: Best way to display Description of Value in Option Set

    Hi e0209,

    You could change the name of the value so that the purpose of the value is clear. Alternatively, add a description to the option set field itself to indicate what the field is used for and explain each value.

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Best way to display Description of Value in Option Set

    Hi e0209,

    You could set a form notification bar in each time of onChange event of Option Set field.(Clear previous notification bar at beginning)

    function onChangeOfOptionSet1() {
      // Clear notification bar
      formContext.ui.clearFormNotification();
      // Set notification bar
      var option = formContext.getAttribute('preferredcontactmethodcode').getSelectedOption().text;
      if (option === 'Email') {
        formContext.ui.setFormNotification("Send email to contact with Outlook", 'INFO')
      } else if (option === 'Phone') {
        formContext.ui.setFormNotification("Keep in touch with contact.", 'INFO')
      }
    }

    When the option is "Email":

    pastedimage1591256106129v1.png

    When the option is "Phone":

    pastedimage1591256149687v2.png

    Regards,

    Clofly

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans