Skip to main content

Notifications

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

  • Verified answer
    shivaram Profile Picture
    3,315 on at
    RE: QuerybyAttribute option set values

    Hi,

    Try to use formatted value for optionset attributes. Then you will get label of related option.

  • David Jennaway Profile Picture
    14,065 on at
    RE: QuerybyAttribute option set values

    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")

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
dkrishna Profile Picture

dkrishna 6

Featured topics

Product updates

Dynamics 365 release plans