Fetchxml to get record count only by parent and not by linked entity - dynamics-crm-2011

Currently I am trying to retrieve count of parent records where condition is governed by linked entity record. When I am trying to run fetchxml without any aggregation record count is coming fine , but when I try to do aggregation it is coming different and bigger in number. on analysis found out that count is taking values from linked entity . Has anyone faced this kind of issue. Following is my fetchxml query :
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" aggregate="true">
<entity name="abc_entity">
<attribute name="abc_name" alias="EntityName" aggregate="countcolumn"/>
<attribute name="createdon" alias="createdonyear" groupby="true" dategrouping="year"/>
<attribute name="createdon" alias="createdonmonth" groupby="true" dategrouping="month"/>
<attribute name="createdon" alias="createdonweek" groupby="true" dategrouping="day"/>
<filter type="and">
<condition attribute="abc_applicationdate" operator="on-or-after" value="#Start_date"/>
<condition attribute="abc_applicationdate" operator="on-or-before" value="#EndDate"/>
<condition attribute="abc_productid" operator="in">
</condition>
</filter>
<link-entity name="abc_entitydocument" from="abc_entityid" to="abc_entityid" alias="av" link-type="inner">
<attribute name="createdon" alias="CreatedDate" aggregate="max"/>
<filter type="and">
<condition attribute="abc_document" operator="not-null"/>
<condition attribute="abc_entityid" operator="not-null"/>
</filter>
</link-entity>
</entity>
</fetch>
Eg. In abc_entity I have linked entity as abc_entitydocument where abc_entitydocument are multiple for each abc_entity. I only want to take count as 1 for those cases which gives me correct count of records.
Thanks

Related

How is the fetchXML Date Operator resolved by the system

CRM 2013-OnPremise
Hello,
I have written a basic View for projects with a start date in the next seven days that is defined by this fetchXML:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" >
<entity name="xxxx_project" >
<attribute name="xxxx_startdate" />
<attribute name="xxxx_accountid" />
<attribute name="xxxx_produom" />
<order attribute="xxxx_name" descending="false" />
<filter type="and" >
<filter type="and" >
<condition attribute="statecode" operator="eq" value="0" />
<condition attribute="xxxx_projectstatus" operator="eq" value="331420009" />
<condition attribute="xxxx_materialsshipdate" operator="null" />
<condition attribute="xxxx_startdate" operator="next-x-days" value="7" />
</filter>
</filter>
<attribute name="xxxx_projectid" />
</entity>
</fetch>
Now I was expecting the fetchXML to have the system date injected into it or at least a place holder such as:
<condition value='2012-03-08T15:10:00Z' />
Perhaps this value is added at runtime and is not part of the fetchTemplate? So Msoft does some runtime changes to something like this?
<condition attribute="xxxx_startdate" operator="next-7-days" value="2014-04-23" />
This article says it is injected http://social.microsoft.com/Forums/en-US/c5083689-65fb-474f-a7bc-2eff393016fe/datetime-on-or-after-of-fetchxml-questions?forum=crmdevelopment
But does not give me any idea of what that should look like.
The reason I ask is that I want to use this basic fetchXML as a template but inject a date of my choosing.
Any idea?
If you want to build dynamically the fetchXML simulating a next 7 days condition based on your date, you can always rewrite it using On or Before and On or After conditions.
For example
<condition attribute="xxxx_startdate" operator="on-or-after" value="2014-04-23" />
<condition attribute="xxxx_startdate" operator="on-or-before" value=2014-04-30" />

Aggregate function with FetchXML

I need to fetch the entity who has the max date in a certain field.
I tried the code below with Stunnware but it gives me an error that the MAX function is invalid.
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' aggregate='true'>
<entity name='field1'>
<attribute name='field2' />
<attribute name='field3' />
<attribute name='field4' />
<order attribute='field1' descending='false' />
<link-entity name='contact' from='field1' to='otherfield' alias='ac'>
<filter type='and'>
<condition attribute='field5' operator='eq' value='123456' />
</filter>
</link-entity>
<link-entity name='secondentity' from='field2' to='otherfield' visible='false' link-type='outer' alias='a_6c61a84be522e31194080050569c4325'>
<attribute name='date' alias='maxdate' aggregate='max' />
</link-entity>
</entity>
</fetch>
Can you help point me to the mistake i'm doing ?
It turned out that it will not work:
There has been several problems in my query:
1- According to Paul Way's reply, my fetch xml was missing aggregate="true"
2- Aggregate functions won't work with Order attribute
3- If I'm going to retrieve attributes while using the aggregate function I have to groupby them and add an alias
4- Aggregate function MAX cannot be applied on Date types.
So my other solution is to retrieve all the dates in descending order and then I will use the first entity retrieved.
Here's a good example:
http://msdn.microsoft.com/en-us/library/gg309565.aspx
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='opportunity'>
<attribute name='estimatedvalue' alias='estimatedvalue_max' aggregate='max' />
</entity>
</fetch>
Should be something like this for your above XML:
<fetch version="1.0" output-format="xml-platform" mapping="logical" aggregate="true">
<entity name="myentity">
<attribute name="personname" />
<order attribute="personname" descending="false" />
<link-entity name="mysecondentity" from="personid" to="secondpersonid" visible="false" link-type="outer" alias="aa">
<attribute name="date" alias='date_max' aggregate="max" />
</link-entity>
</entity>
</fetch>
I just got had the same experience. To make a long story short: SUM, AVG, MIN and MAX are not possible for date fields in CRM 2011. In CRM 2013 MIN and MAX are working for date fields.
So in 2011, the only way is to either select everything and do the work on your own or select the data, order by it and set pagesize to 1, to get just the max or min value.

FetchXML View to Include Attributes from Nested Link-Entity

I would like to have a view that show attributes from 3 entities:
Statistics has a lookup to Account and Account has a lookup to Address.
The view is on Statistics and I want attributes from all 3 entities; is this even possible?
The problem is with the GridXML.
I want to include the attribute wl_city in the GridXML.
This is the FetchXML with link-entities:
<fetchxml>
<fetch version="1.0" output-format="xml-platform" mapping="logical">
<entity name="sb_statistics">
<order attribute="sb_amount" descending="false" />
<!-- It is easy to get these into the GridXML -->
<attribute name="sb_debtor" />
<attribute name="sb_date" />
<attribute name="sb_amount" />
<link-entity name="account" from="accountid" to="sb_debtor"
alias="relatedAccount" link-type="outer">
<!-- It is possible to get this into the GridXML
by using the link-entity alias: relatedAccount.wl_towncity -->
<attribute name="wl_towncity" />
<link-entity name="wl_postalcode" from="wl_postalcodeid"
to="wl_postaltowncity" alias="relatedAddress" link-type="outer">
<!-- I have trouble getting this attribute into the GridXML -->
<attribute name="wl_city" />
</link-entity>
</link-entity>
<attribute name="sb_statisticsid" />
</entity>
</fetch>
</fetchxml>
When I change the GridXML as below this error is displayed when the view is opened:
"To use this saved query, you must remove criteria and columns that refer to deleted or non-searchable items"
<layoutxml>
<grid name="resultset" object="10008" jump="sb_name" select="1" preview="1"
icon="1">
<row name="result" id="sb_statisticsid" multiobjectidfield="1">
<cell name="sb_amount" width="100" />
<cell name="sb_date" width="100" />
<cell name="sb_debtor" width="100" />
<cell name="relatedAccount.relatedAddress.wl_city" width="100" />
</row>
</grid>
</layoutxml>
The below GridXML shows this error when the view is opened:
"Unexpected Error An error has occured".
<layoutxml>
<grid name="resultset" object="10008" jump="sb_name" select="1" preview="1"
icon="1">
<row name="result" id="sb_statisticsid" multiobjectidfield="1">
<cell name="sb_amount" width="100" />
<cell name="sb_date" width="100" />
<cell name="sb_debtor" width="100" />
<cell name="relatedAddress.wl_city" width="100" />
</row>
</grid>
</layoutxml>
The GridXML below results in this error being shown when the view is opened:
"To use this saved view, you must remove criteria and columns that refer to deleted or non-searchable columns".
<layoutxml>
<grid name="resultset" object="10008" jump="sb_name" select="1" preview="1"
icon="1">
<row name="result" id="sb_statisticsid" multiobjectidfield="1">
<cell name="sb_amount" width="100" />
<cell name="sb_date" width="100" />
<cell name="sb_debtor" width="100" />
<cell name="wl_city" width="100" />
</row>
</grid>
</layoutxml>
This saved query works, but it only includes attributes from the primary entity and the first link-entity.
<savedquery>
<IsCustomizable>1</IsCustomizable>
<CanBeDeleted>0</CanBeDeleted>
<isquickfindquery>0</isquickfindquery>
<isprivate>0</isprivate>
<isdefault>0</isdefault>
<returnedtypecode>10008</returnedtypecode>
<savedqueryid>{df101ac4-2e4d-e311-9377-005056bd0001}</savedqueryid>
<layoutxml>
<grid name="resultset" object="10008" jump="sb_name" select="1" preview="1"
icon="1">
<row name="result" id="sb_statisticsid" multiobjectidfield="1">
<cell name="sb_amount" width="100" />
<cell name="sb_date" width="100" />
<cell name="sb_debtor" width="100" />
<cell name="relatedAccount.wl_city" width="100" />
</row>
</grid>
</layoutxml>
<querytype>0</querytype>
<fetchxml>
<fetch version="1.0" output-format="xml-platform" mapping="logical">
<entity name="sb_statistics">
<order attribute="sb_amount" descending="false" />
<attribute name="sb_debtor" />
<attribute name="sb_date" />
<attribute name="sb_amount" />
<link-entity name="account" from="accountid" to="sb_debtor"
alias="relatedAccount" link-type="outer">
<attribute name="wl_towncity" />
<link-entity name="wl_postalcode" from="wl_postalcodeid"
to="wl_postaltowncity" alias="relatedAddress" link-type="outer">
<attribute name="wl_city" />
</link-entity>
</link-entity>
<attribute name="sb_statisticsid" />
</entity>
</fetch>
</fetchxml>
<LocalizedNames>
<LocalizedName description="Statistics and Address" languagecode="1033" />
</LocalizedNames>
</savedquery>
Is GridXML limited to showing only attributes from the primary entity and the first link-entity?
This is not possible, according to the best of my knowledge, but please someone prove me wrong.
A limitation of GridXML appears to be that attributes can only be included that are from the first link-entity, not any nested link-entities.
It should work when using link-type="inner" for nested link.
<entity name="sb_statistics">
...
<link-entity name="account" from="accountid" to="sb_debtor"
alias="relatedAccount" link-type="outer">
<attribute name="wl_towncity" />
<link-entity name="wl_postalcode" from="wl_postalcodeid"
to="wl_postaltowncity" alias="relatedAddress" link-type="inner"> //link-type="inner"
<attribute name="wl_city" />
</link-entity>
</link-entity>
<attribute name="sb_statisticsid" />
</entity>
I have found no evidence that it can be done. With or without link-type='inner' the designer (in 2013) says, "The relatedAddress.wl_city column is no longer a valid column because it has been deleted as a column option. You need to remove this column and, if you want, add a different one."
It does NOT need multiple dereferrences, nor does that work. If you dump the keyValuePairs of the AttributeCollection returned by the fetch, you will see the key is relatedAddress.w1_city -- not its parent nor the combination.
Like the UI, it just appears the layout is limited to only root and children, no grandchildren nor further descendants.
I think it's a little late to answer this question, but maybe someone come to this post and find it helpful.
first thing you should know is that, fetchxml will return only column that are not null, so if you are querying a column that there is no data in that, then fetchxml automatically remove it from result set.
second thing is, if you have different table with different relationship, then alias name will be added to the column name, so in your case relatedAccount.wl_towncity and relatedAddress.wl_city is correct and not relatedAccount.relatedAddress.wl_city. in your example, you put alias name after each other that is not correct.
third thing that you should know is that when a nested result will return, the type is object, but original type AliasedValue , so first you have to cast the object to AliasedValue. then it become ready to cast it to OptionSetValue. after that you have to look for .Value that has the result of what you want
I made it work like this: I still have an issue with unresolved columnheaders.
<fetch distinct='true'>
<entity name='rdiac_riskobject'>
<attribute name='rdiac_riskobjectid' />
<attribute name='rdiac_name' />
<attribute name='rdiac_riskobjectproduct' />
<link-entity name='rdiac_riskobject_rdiac_propertydetail' from='rdiac_riskobjectid' to='rdiac_riskobjectid' intersect='true'>
<link-entity name='rdiac_propertydetail' alias='pd1' from='rdiac_propertydetailid' to='rdiac_propertydetailid'>
<attribute name='rdiac_valuestring' />
<link-entity name='rdiac_propertysvconfig' from='rdiac_property' to='rdiac_propertyid'>
<filter>
<condition attribute='rdiac_svfield' operator='eq' value='100000000'/>
</filter>
</link-entity>
</link-entity>
</link-entity>
<link-entity name='rdiac_riskobject_rdiac_propertydetail' from='rdiac_riskobjectid' to='rdiac_riskobjectid' intersect='true'>
<link-entity name='rdiac_propertydetail' alias='pd2' from='rdiac_propertydetailid' to='rdiac_propertydetailid'>
<attribute name='rdiac_valuestring' />
<link-entity name='rdiac_propertysvconfig' from='rdiac_property' to='rdiac_propertyid'>
<filter>
<condition attribute='rdiac_svfield' operator='eq' value='100000001'/>
</filter>
</link-entity>
</link-entity>
</link-entity>
</entity>
</fetch>
<grid name='resultset' object='10139' jump='rdiac_riskobjectproduct' select='1' preview='0' icon='1' >
<row name='result' id='rdiac_riskobjectid' >
<cell name='rdiac_riskobjectproduct' width='100' />
<cell name='pd1.rdiac_valuestring' width='200' />
<cell name='pd2.rdiac_valuestring' width='200' />
</row>
</grid>

FetchXML Filters With Different Entities

I wish to filter my entities in CRM2011 using a fetchXML filter. However, I'm having issues with AND and OR groupings over different entities.
I am searching for clients based on their consent, where each client will have either 1 or 0 valid consents. I want to return the client if there is no valid consent. I also want clients returned if they have a consent, but not if the have a 'restricted' consent without the agency specified (eg. client.consent.type == 'restricted' AND client.consent.users CONTAINS user)
So far I have this:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
<entity name="contact">
<attribute name="fullname" />
<attribute name="thr_verifiedproofofidentity" />
<attribute name="thr_interpreterrequired" />
<attribute name="emailaddress1" />
<attribute name="thr_consent" />
<attribute name="birthdate" />
<attribute name="thr_individualreferencenumber" />
<attribute name="contactid" />
<order attribute="fullname" descending="false" />
<filter type="and">
<condition attribute="statecode" operator="eq" value="0" />
<condition attribute="thr_consent" operator="not-null" />
</filter>
<link-entity name="thr_consent" from="thr_clientid" to="contactid" alias="aa">
<filter type="and">
<filter type="or">
<condition attribute="thr_consenttype" operator="eq" value="130120003" />
<condition attribute="thr_consenttype" operator="ne" value="130120003" /> *
</filter>
</filter>
<link-entity name="thr_thr_consent_thr_agency" from="thr_consentid" to="thr_consentid" visible="false" intersect="true">
<link-entity name="thr_agency" from="thr_agencyid" to="thr_agencyid" alias="ab">
<filter type="and">
<condition attribute="thr_agencyid" operator="eq" uiname="Test" uitype="thr_agency" value="(agency id goes here)" /> *
</filter>
</link-entity>
</link-entity>
</link-entity>
</entity>
</fetch>
The only thing missing from this code is that I need an AND grouping for the two condition attributes market with the '*'.
Can this be done?
As Guido Preite points out, it would be easiery to use the Advanced find to create the Fetch Xml, rather than hand editing it.
But...
Since you're not doing any grouping, I would suggest not even using Fetch Xml, but instead go with one of the other supported SDK options (QueryExpressions, Linq to CRM, oData, etc).
I'm not sure I completely understand your request (if you could write your query as a SQL statement, it would be helpful), but I think you'll need to have 2 thr_consent link entity of link-type="outer". The first has your equal to 130120003 condition, the second has your links to the agency id condition.

MS CRM 2011 Fetch XML Query

I'm trying to create some fetch xml or a query expression for CRM 2011.
I would like to OR the two linked entity nodes below. Is this possible at all, I need to do it in one request.
If I can perform this query I intend to modify the Activity History RetrieveMultiple views events by injecting extra criteria, similar to below.
<fetch mapping='logical' distinct='true'>
<entity name='activitypointer'>
<attribute name='activitytypecode' />
<attribute name='subject' />
<attribute name='statecode' />
<attribute name='prioritycode' />
<attribute name='modifiedon' />
<attribute name='activityid' />
<attribute name='instancetypecode' />
<order attribute='modifiedon' descending='false' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='1' />
</filter>
<link-entity name='activityparty' from='activityid' to='activityid' alias='aa'>
<link-entity name='account' from='accountid' to='partyid' alias='ab'>
<filter type='or'>
<condition attribute='accountid' operator='eq' uiname='A new Trust' uitype='account' value='{756CE4E9-F6F0-E111-8948-000C297B9BDA}' />
</filter>
</link-entity>
</link-entity>
<link-entity name='connection' from='record2id' to='activityid' alias='ad'>
<link-entity name='account' from='accountid' to='record1id' alias='ae'>
<filter type='or'>
<condition attribute='accountid' operator='eq' uiname='A new Trust' uitype='account' value='{756CE4E9-F6F0-E111-8948-000C297B9BDA}' />
</filter>
</link-entity>
</link-entity>
</filter>
</entity>
</fetch>
Here's the SQL that brings back the results I'm looking for, please note that the issue issue is recreating the fetchXml for the where clause, specifically checking that one of the joins exist.
SELECT DISTINCT
ap.activitytypecode,
ap.[subject],
ap.statecode,
ap.prioritycode,
ap.modifiedon,
ap.activityid,
ap.instancetypecode
FROM
dbo.FilteredActivityPointer ap
--First Link
LEFT OUTER JOIN dbo.FilteredActivityParty party
ON ap.activityid = party.activityid
AND party.partyid = '756CE4E9-F6F0-E111-8948-000C297B9BDA'
--Second Link
LEFT OUTER JOIN dbo.FilteredConnection connection
ON ap.activityid = connection.record2id
AND connection.record1id = '756CE4E9-F6F0-E111-8948-000C297B9BDA'
WHERE
ap.statecode =1
AND (
NOT party.partyid IS NULL
OR NOT connection.record1id IS NULL
)
ORDER BY
ap.modifiedon
Please help.
If I'm way off the mark, maybe you can write the basic SQL statement that you're looking to achieve. But I think you're wanting to return all Activity Pointers where a particular account is a connection or activity party? If so, then you'll need to change your links to be outer links.
Try adding the link-type='outer' attribute and value to your link entities like so:
<fetch mapping='logical' distinct='true'>
<entity name='activitypointer'>
<attribute name='activitytypecode' />
<attribute name='subject' />
<attribute name='statecode' />
<attribute name='prioritycode' />
<attribute name='modifiedon' />
<attribute name='activityid' />
<attribute name='instancetypecode' />
<order attribute='modifiedon' descending='false' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='1' />
</filter>
<link-entity name='activityparty' from='activityid' to='activityid' alias='aa' link-type='outer'>
<link-entity name='account' from='accountid' to='partyid' alias='ab' link-type='outer'>
<filter type='or'>
<condition attribute='accountid' operator='eq' uiname='A new Trust' uitype='account' value='{756CE4E9-F6F0-E111-8948-000C297B9BDA}' />
</filter>
</link-entity>
</link-entity>
<link-entity name='connection' from='record2id' to='activityid' alias='ad' link-type='outer'>
<link-entity name='account' from='accountid' to='record2id' alias='ae' link-type='outer'>
<filter type='or'>
<condition attribute='accountid' operator='eq' uiname='A new Trust' uitype='account' value='{756CE4E9-F6F0-E111-8948-000C297B9BDA}' />
</filter>
</link-entity>
</link-entity>
</filter>
</entity>
</fetch>
Edit 1
After looking at your SQL query, I believe what you're attempting to do is not supported in CRM in a single query, although right now I can't find any documentation to back up my hunch... I don't think you can do a count of the outer joined records then say, only return where the count of the first Link Entity + the count of the second Link Entity > 0, but I could be mistaken.

Resources