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)

How to Add an optionset filter criteria in MS CRM Query Expression?

(1) ShareShare
ReportReport
Posted on by

I have an entity LeaveType with two attributes, 1. Type, 2. Available Days, where Type is an optionset and Available days is a text field. I want to fetch all such LeaveType records where the Type = 'Annual' selected in the optionset. I am not able to find how to add a filter the query expression for the option set value. Below is my in progress method:

public Entity Getleavetype(Guid LeaveDetailsId, IOrganizationService _orgService, CodeActivityContext Acontext)
        {
            QueryExpression GetLeavedetails = new QueryExpression();
            GetLeavedetails.EntityName = "sgfdhr_leavetype";
            GetLeavedetails.ColumnSet = new ColumnSet("new_type");
            GetLeavedetails.ColumnSet = new ColumnSet("new_availabledays");
            GetLeavedetails.Criteria.AddCondition("new_type", ConditionOperator.Equal,   "Annual" ); //Is this correct????
            GetLeavedetails.Criteria.AddCondition("new_employeeleavecalculation", ConditionOperator.Equal, LeaveDetailsId); //ignore this

            //((OptionSetValue)LeaveDetailsId["new_leavetype"]).Value

            EntityCollection LeaveDetails = _orgService.RetrieveMultiple(GetLeavedetails);
            return LeaveDetails[0];
        }


Please advise.

Thanks, 

Fahad Ali Shaikh

*This post is locked for comments

I have the same question (0)
  • Guido Preite Profile Picture
    54,086 Moderator on at

    In your condition you need to set the integer value of the optionset, not the label.

    Assuming that Annual value is for example 2, the code will be:

    GetLeavedetails.Criteria.AddCondition("new_type", ConditionOperator.Equal, 2);

  • Kevin T 9 Profile Picture
    105 on at

    How do you check which integer value corresponds with a given text value?  Is it assumed you'll figure it out and hard code the value, or is there a way to look up the option set key/value pairs?

  • Community Member Profile Picture
    on at

    Hi kevin,

    You can check the optionset Value and Text  in the field  properties.

    1_2D00_Division_2D00_Option_2D00_Set.png

    Hope it helps :)

    Thanks ,

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