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)

QuerybyAttribute option set values

(0) ShareShare
ReportReport
Posted on by 1,703

I have been trying to get the values of optionsets but get the an error  Unable to cast object of type 'Microsoft.Xrm.Sdk.OptionSetValue' to type 'System.String'.

I have checked that the record exists ( in raw sql ) and that both optionset names are correct and in the correct case

                    Dim ReturnedSoftware As String = String.Empty
                    Dim ReturnedLicenceStatus As String = String.Empty
                    Dim AccountList As New QueryByAttribute("account")
                    AccountList.ColumnSet = New ColumnSet("new_service", "new_licencestatus")
                    AccountList.Attributes.AddRange("accountid")
                    AccountList.Values.AddRange(LicenceID)
                    Dim ReturnedAccountList As EntityCollection = service.RetrieveMultiple(AccountList)
                    For Each ReturnedAccount As Entity In ReturnedAccountList.Entities

                        If ReturnedAccount.Attributes.ContainsKey("new_service") Then
                            ReturnedSoftware = DirectCast(ReturnedAccount.Attributes("new_service"), String)
                        End If

                        If ReturnedAccount.Attributes.ContainsKey("new_licencestatus") Then
                            ReturnedLicenceStatus = DirectCast(ReturnedAccount.Attributes("new_licencestatus"), String)
                        End If
                        Exit For
                    Next

*This post is locked for comments

I have the same question (0)
  • David Jennaway Profile Picture
    14,065 on at

    Are you trying to get the integer value of the option sets, or the label ?

    For the integer value, you'll need to cast to an OptionSetValue, then use the Value property

    For the label, you need to use the FormattedValues collection, e.g.:

     ReturnedSoftware = ReturnedAccount.FormattedValues("new_service")

  • Verified answer
    shivaram Profile Picture
    3,315 on at

    Hi,

    Try to use formatted value for optionset attributes. Then you will get label of related 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

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