Retrieving the OptionSet Label using JavaScript
I was adding some custom JavaScript to a customer's Opportunity Entity when I, again, learned the importance of not over-thinking things.
I was concatenating three field values and using that combined value to set the value of another field. Unfortunately, one of those fields was an OptionSet.
The problem with OptionSets is that when you retrieve the value of the field, using the following command:
var fieldValue = Xrm.Page.getAttribute("field").getValue()
returns the integer value of the field, not the textual label displayed on the form – and it was the latter that I needed.
So I'm thinking to myself, "Man, now I have to go and get the metadata for that OptionSet and pull out the label." That is not a lot of fun and not exactly performant either, so I would need to optimize the way I would retrieve and store this data. Again, not a lot of fun.
Then it occurred to me: I didn't need to get the Label for just any value in the OptionSet, just the one that was selected.
And it was the Xrm.Page object model to my rescue.
This command will retrieve the currently selected Label from an OptionSet on the form:
var fieldValue = Xrm.Page.getAttribute("field").getText()
This worked just fine for my purposes, and it might for yours as well.
References:
This was originally posted here.
*This post is locked for comments