We'll use the IOrganizationService RetrieveMultiple method with a FilterExpression and a ConditionExpression, to get only one record: the latest one created . We also check an attribute for null values .
Usually this C# code will be run from a Dynamics CRM Plugin.
Download the C# code from the following GitHub repository:
We'll see here in only 10 minutes how to write the CRM query , which will be shown this way , for example :
CRM RetrieveMultiple QueryExpression to retrieve the top one last created record/last modified record
First , you need to set which columns to retrieve. It is strongly recommended that you do not select all columns in an of the Microsoft Dynamics CRM SDK entity :
Next, we create the QueryExpression that will hold the query to be send to the OrganizationService , as follows :
There, we add an OrderExpression to sort the records acording to its "createdon" or "modifiedon" attribute.
Then, we cut the results to just the first result and circumscribe them to the first page:
Then, we can check some determined attribute to contain a value - with the not null ConditionOperator:
And include all conditions in a FilterExpression, into the Criteria:
The complete query will look something like this (this code has been tested , and works very well) :
The steps are as follows:
0) Set which columns to retrieve.
1) Create the QueryExpression
2) Add an OrderExpression
3) Check some determined attribute to contain a value using a ConditionOperator (optional)
4) Include all conditions inside the Criteria as a FilterExpression
5) Send a RetrieveMultiple with the QueryExpression
by Carmel Schvartzman
כתב: כרמל שוורצמן