Showing posts with label Fetch XML;Query Expression. Show all posts
Showing posts with label Fetch XML;Query Expression. Show all posts

Sunday, May 29, 2011

How to get the Total Results returned Record Count for a given query

In CRM2011 it is now possible to get the Total count of records returned by a query using Fetch XML.

Using Query expression:

You can set the ReturnTotalRecordCount property to True.



Using FetchXml:

You can set the same property as shown below.

<fetch mapping='logical' count='10' returntotalrecordcount='true'>
<entity name='account'>
<attribute name='accountid' />
<attribute name='accountnumber' />
<attribute name='address1_city' />
<attribute name='address1_telephone1' />
<attribute name='name' />
<order attribute='name' />
<filter>
<condition attribute='address1_city' operator='like' value='a%' />
</filter>
</entity>
</fetch>

This can be helpful to show Record 1 of kind of status labels.

Hope this helps!

Friday, April 9, 2010

FetchXML v/s QueryExpression

These are the two distinct ways that you can query CRM using CRM SDK to retrieve the required information.

QueryExpression is the mostly commonly used way for querying information in CRM. Probably because it uses the object oriented style of coding and so you have distinct classes for query, condition, columns etc. Youhave intellisense to support you when writing a query using QueryExpression.

However, it has its own limitations, one of them being the inability to provide a column of a linked entity to be returned as the query result. This perhaps because the Query Expression would return a dynamic entity or a strongly typed base entity and so it is unable to return columns of related entity.

So you are not able to execute a query similar to the following

Select Opportunity.name, Opportunity.estimatedvalue, Account.address1_city from FilteredOpportunity Opportunity
Inner join
FilteredAccount Account on Opportunity.customerid = Account.accountis
Where account.address1_country = “US”

This is however possible using FetchXML. FetchXML requires the query to be specified in XML format and the resultset is returned in XML format as well. This allows FetchXML to return related entity columns as well as this is just another node in the xml doc.

The above select query can written as follows using FetchXML

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="opportunity">
<attribute name="name"/>
<attribute name="estimatedvalue"/>
<attribute name="opportunityid"/>
<order attribute="name" descending="false"/>
<link-entity name="account" from="accountid" to="customerid" alias="aa">
<attribute name="address1_city"/>
<filter type="and">
<condition attribute="address1_country" operator="eq" value="US"/>
</filter>
</link-entity>
</entity>
</fetch>

Note: CRM Views allow you to select columns of related entity to be displayed in the view… how do you think they do it??? FectchXML ofcourse. The query of the view created is stored as a FetchXML query in the SavedQuery entity.

CRM also provides the following messages to convert FetchXML to QueryExpression and vice versa.

- FetchXmlToQueryExpression : Converts from FetchXML to query expression.
- QueryExpressionToFetchXml : Converts from query expression to FetchXML.

Note again, if you are to convert a FetchXML that has select columns specified from related entity, to Query Expression, the related entity columns are not included in the QueryExpression columns list.

Hope this helps to smart querying!!!