Sunday, October 7, 2012

Adding Conditions in Query Expression

Problem:
              In previous post related to the query expression I have added paging in query expression to get paged data, now need to add some condition so that we get the records based on some conditions.
Solution:
              When working with data retrieval there are some condition based on which you have to fetched the records, if there is not client specific conditions then there must be at least one condition to get the active records.Image for the sample application is shown in Image 1 as shown below, here you can see that I have displayed only the active records (I have deactivated the 3 records so that I have some active and deactivate records).
Image 1

The code for the sample which I have added is shown in the List 1 , here you can see that I have first created and initialized the condition expression object and then I have set the name of the attribute which I want to compare here I have mention the “statecode” mean I want to check the is record is active or not (you have to mention the schema name of the attribute here). Then next is the name of the operator which I will use for this condition and here you can see that I have used the ConditionOperator enumeration to select the operator which I will use for this condition and I have used equal operator. At the end is the values attribute which you need to set here as I have used equal operator so I will add just one value 0 ( mean I just want to get all the active records). You can add multiple values in the Values property the operator which may need more than one value are “in” operator or “between” operator.

List 1
Next is the filter expression object which is used to add the condition which we have created just above , the filter expression class has the filter operation which will be either “or” operator or “And” operation I have used “And” operator mean no matter how many condition I will add to this filter expression object but we can just set one filter operator which will be applied on that condition if you want to set different filter operator then you have to create another filter operation object and add the condition in that filter expression object.After adding condition to the filter expression next is to add the filter expression to the query expression.
Hope you get an idea how to add condition to the query expression as conditions are the most important part of any query. You can add as many condition as you can but remember in one filter expression you can add only one filter operation which may be “Or” or “And” operation.Below are the links for the download for the source code and MS Crm 2011 solution.

Downloads
You can download the source code of the sample from here.
and working CRM 2011 solution from here.

Go to Home

All and any comments / bugs / suggestions are welcomed!

No comments: