Dynamics crm fetchxml group by - dynamics-crm

I would like to have the total number of tasks for each user. For example, John Smith 562, Elsa Taylor 953, etc. I tried this fetchxml, but the result is just the total number of tasks:
<fetch aggregate="true" >
<entity name="task" >
<link-entity name="systemuser" from="systemuserid" to="ownerid" >
<attribute name="fullname" alias="fullname" aggregate="count" />
</link-entity>
</entity>
</fetch>
How can I accomplish this?

You need to switch your query around and select (and group by) the systemuser fullname and count the tasks:
<fetch aggregate="true" >
<entity name="systemuser" >
<attribute name="fullname" alias="fullname" groupby="true" />
<link-entity name="task" from="ownerid" to="systemuserid" link-type="inner" alias="t" >
<attribute name="activityid" alias="numberoftasks" aggregate="count" />
</link-entity>
</entity>
</fetch>

Related

FetchXML or condition on the attributes of two related entities

The requirements for this FetchXML statement are:
Return all 'ebs_opportunityinternshipunit' where
'createdon' is 'last-week'
and
'owner' of 'account' is 'eq-userid'
'account' is referenced by 'parentaccountid' of 'opportunity'
or
'owner' of 'account2' is 'eq-userid'
'account2' is referenced by 'mitacs_partner2' of 'opportunity'
I've tried configuring the filter with out-of-the-box tools, XRMToolbox FetchXML Builder, visiting the official CRM Community forum, and reading FetchXML documentation. Still, the best that I've been able to come up with is below:
<fetch version="1.0" mapping="logical">
<entity name="ebs_opportunityinternshipunit">
<attribute name="ebs_name" />
<attribute name="createdon" />
<attribute name="ebs_opportunityinternshipunitid" />
<order attribute="ebs_name" descending="false" />
<filter type="and">
<condition attribute="createdon" operator="last-week" />
<filter type="or">
<condition entityname="ab" attribute="ownerid" operator="eq-userid" />
<condition entityname="ac" attribute="ownerid" operator="eq-userid" />
</filter>
</filter>
<link-entity name="opportunity" from="opportunityid" to="ebs_opportunity" link-type="inner" alias="aa">
<link-entity name="account" from="accountid" to="parentaccountid" link-type="inner" alias="ab" />
<link-entity name="account" from="accountid" to="mitacs_partner2" link-type="inner" alias="ac" />
</link-entity>
</entity>
</fetch>

Select a count of contacts from accounts using FetchXML

Does anyone know the FetchXML format in order to select the count of contacts for accounts?
For example, I would have a list of AccountID's that I would us an IN filter for and I would just require the AccountID and the integer count of the contacts for that account.
Solved (see below) with a minor change to group by accounts. Here is the final fetchxml that I used:
<fetch xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" aggregate="true" distinct="false" mapping="logical">
<entity name="contact">
<attribute name="contactid" alias="recordcount" aggregate="count" />
<link-entity name="account" to="accountid" alias="accountid">
<attribute name="accountid" alias="accountid" groupby="true" />
<filter>
<condition attribute="accountid" operator="in">
<value>708039fd-f7b1-e811-a973-000d3af4a510</value>
<value>0a8139fd-f7b1-e811-a973-000d3af4a510</value>
<value>428139fd-f7b1-e811-a973-000d3af4a510</value>
<value>4a8139fd-f7b1-e811-a973-000d3af4a510</value>
<value>618139fd-f7b1-e811-a973-000d3af4a510</value>
<value>9f8139fd-f7b1-e811-a973-000d3af4a510</value>
<value>ae8239fd-f7b1-e811-a973-000d3af4a510</value>
</condition>
</filter>
</link-entity>
</entity>
</fetch>
You can do an aggregate fetch with a group by on the parentcustomerid.
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='contact'>
<attribute name='contactid' alias='contact_count' aggregate='countcolumn' />
<attribute name='parentcustomerid' alias='parentcustomerid' groupby='true' />
<link-entity name='account' from='accountid' to='parentcustomerid'>
<filter type='and'>
<condition attribute='accountid' operator='in'>
<value>{00000000-0000-0000-0000-000000000001}</value>
<value>{00000000-0000-0000-0000-000000000002}</value>
</condition >
</filter>
</link-entity>
</entity>
</fetch>
This is straight forward when you use Rollup field.
Then you can query the count easily.
<fetch>
<entity name="account" >
<attribute name="name" />
<attribute name="new_contactcountrollup" />
</entity>
</fetch>
Update:
Why I said subquery is not supported in fetchxml since I was thinking the second solution.
--using just JOIN
SELECT a.accountid, count(1) AS [contact count] FROM contact c
INNER JOIN account a
ON c.parentcustomerid = a.accountid
WHERE a.accountid IN (
'{0ACDC4F5-4885-E811-A967-000D3A1A9407}', '{BA41CEBA-199F-E811-A96B-000D3A1A9EFB}')
GROUP BY a.accountid
--using SUBQUERY
SELECT a.accountid,
(SELECT Count(1) FROM contact c WHERE c.parentcustomerid = a.accountid) AS [contact count]
FROM account a
WHERE a.accountid IN (
'{0ACDC4F5-4885-E811-A967-000D3A1A9407}', '{BA41CEBA-199F-E811-A96B-000D3A1A9EFB}')

How to filter using fetch to get accurate results?

I need to get all accounts that have phonecalls in state not open, so I created a query on fetch and got some results.
After checking my results I found that I got all accounts that have minimum 1 phonecall that was not open, but I need to get the accounts that all of their connected phonecalls are not open (can't have even 1 in open state) is it possible to do by fetch ?
** by NOT OPEN I mean state of Canceled or Completed.
Here is my fetch query:
#"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
<entity name='account'>
<attribute name='name' />
<order attribute='accountamount' descending='true' />
<link-entity name='phonecall' from='regardingobjectid' to='accountid' alias='ab'>
<filter type='and'>
<condition attribute='statecode' operator='ne' value='0' />
</filter>
</link-entity>
</entity>
</fetch>";
What you are looking for is Unmatch query. This can be achieved in 2 queries using fetchxml.
First Query: You have to pull all the Accounts with open phonecalls.
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >
<entity name="account" >
<attribute name="accountid" />
<order attribute="accountamount" descending="true" />
<link-entity name="phonecall" from="regardingobjectid" to="accountid" alias="ab" >
<filter type="and" >
<condition attribute="statecode" operator="eq" value="0" />
</filter>
</link-entity>
</entity>
</fetch>
Second Query: Iterate & pass the first query Account resultset as <value> like below, to filter out them.
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >
<entity name="account" >
<attribute name="name" />
<attribute name="primarycontactid" />
<attribute name="telephone1" />
<attribute name="accountid" />
<order attribute="name" descending="false" />
<filter type="and" >
<condition attribute="accountid" operator="not-in" >
<value><GUID of ACCOUNT1 with OPEN PHONECALL></value>
<value><GUID of ACCOUNT2 with OPEN PHONECALL></value>
<value><GUID of ACCOUNT3 with OPEN PHONECALL></value>
</condition>
</filter>
</entity>
</fetch>
Read for idea

CRM Reports, Why,Some Address fields not populating?

I known this a broad question, I just cant get my head around it. I have created a report that pulls client details for a specific sales person. I get back clients with addresses and some without addresses , even though the address composite field has been filled out on CRM. There is no equation/expression that I have used to pull the address date. Just get the field and display. Can you help me , by giving me a few pointers on what to check/look out for in CRM .
UPDATE 1:
<fetch distinct="false" no-lock="false" mapping="logical">
<entity name="activitypointer" enableprefiltering="1" prefilterparametername="CRM_FilteredActivityPointer">
<attribute name="scheduledstart" alias="scheduledstart" />
<attribute name="subject" alias="subject" />
<attribute name="regardingobjectid" alias="regardingobjectid" />
<attribute name="description" alias="description" />
<attribute name="activityid" />
<attribute name="ownerid" alias="ownerid" />
<attribute name="activitytypecode" />
<attribute name="actualstart" alias="actualstart" />
<attribute name="actualdurationminutes" alias="actualdurationminutes" />
<attribute name="actualend" alias="actualend" />
<link-entity name="opportunity" to="regardingobjectid" from="opportunityid" link-type="outer" alias="LE_3c8631e7bcbe64b3de96e66789a47536">
<attribute name="customerid" alias="LE_3c8631e7bcbe64b3de96e66789a47536_customerid" />
<attribute name="schedulefollowup_qualify" alias="LE_ad91c354eb5a26b004de4d41b2c3d454_schedulefollowup_qualify" />
<attribute name="new_accounttype" alias="LE_54d761d89ac278139a6836de7c3607db_new_accounttype" />
<attribute name="new_neworexisiting" alias="LE_5db2d6da9e43bd44c8949cb912432cba_new_neworexisiting" />
<attribute name="new_accaddresscomposite" alias="LE_c6b3b5d2a9364c90878a5d147b97b866_new_accaddresscomposite" />
<attribute name="parentaccountid" alias="LE_b7d5c9432034544323d9170db9688778_parentaccountid" />
<attribute name="actualclosedate" alias="LE_3d1bcadc4e9010d6d4689ded2531d68a_actualclosedate" />
</link-entity>
<link-entity name="account" to="regardingobjectid" from="accountid" link-type="outer" alias="LE_d08b5ac0b1b1a422353a6d092b699986">
<attribute name="name" alias="LE_d08b5ac0b1b1a422353a6d092b699986_name" />
</link-entity>
<link-entity name="account" to="regardingobjectid" from="accountid" link-type="outer" alias="LE_b6831392115770835cce64e99a59be4a">
<attribute name="address1_composite" alias="LE_b6831392115770835cce64e99a59be4a_address1_composite" />
</link-entity>
<link-entity name="new_visit" to="regardingobjectid" from="new_visitid" link-type="outer" alias="LE_041a4de02adecc6816a3be5b9389d9ac">
<attribute name="new_tmpdurationmins" alias="LE_041a4de02adecc6816a3be5b9389d9ac_new_tmpdurationmins" />
</link-entity>
<link-entity name="new_visit" to="regardingobjectid" from="new_visitid" link-type="outer" alias="LE_0238625bc02c72e091b42d4c6ece34e5">
<attribute name="new_arriveddatetime" alias="LE_0238625bc02c72e091b42d4c6ece34e5_new_arriveddatetime" />
</link-entity>
</entity>
As a first tweak I'd suggest you merge those separate link-entity joins on account into one:
current joins on account:
<link-entity name="account" to="regardingobjectid" from="accountid" link-type="outer" alias="LE_d08b5ac0b1b1a422353a6d092b699986">
<attribute name="name" alias="LE_d08b5ac0b1b1a422353a6d092b699986_name" />
</link-entity>
<link-entity name="account" to="regardingobjectid" from="accountid" link-type="outer" alias="LE_b6831392115770835cce64e99a59be4a">
<attribute name="address1_composite" alias="LE_b6831392115770835cce64e99a59be4a_address1_composite" />
</link-entity>
proposed change:
<link-entity name="account" to="regardingobjectid" from="accountid" link-type="outer" alias="LE_d08b5ac0b1b1a422353a6d092b699986">
<attribute name="name" alias="LE_d08b5ac0b1b1a422353a6d092b699986_name" />
<!-- move the address1_composite field here -->
<attribute name="address1_composite" alias="LE_d08b5ac0b1b1a422353a6d092b699986_composite" />
</link-entity>
This should at least yield a consistent behavior for all account fields but it still leaves open if using activitypointer as the root entity for your report is the right way. It depends on your reports business logic/user story.
You could verify this by formulating the report in plain language: "For all Activities, show me ..." vs. "For all Opportunities, show me ..." vs. "For all Accounts, show me ..."
A great way to test your reports FetchXml query is using FetchXmlTester contained in Xrm Toolbox.

Can you write a single FetchXML query to get 1:many relationship?

Is it possible to write a single FetchXML query that gets a root entity and multiple children? All I've been able to do is 1:1.
James Wood is correct. Fetch XML is recursive so by using the link entity you can get the information you want.
For example, the following is valid:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
<entity name="account">
<attribute name="name" />
<attribute name="primarycontactid" />
<attribute name="telephone1" />
<attribute name="accountid" />
<order attribute="name" descending="false" />
<link-entity name="contact" from="parentcustomerid" to="accountid" alias="aj">
<attribute name="firstname" />
<attribute name="lastname" />
<attribute name="telephone1" />
<link-entity name="businessunit" from="businessunitid" to="owningbusinessunit" alias="ak">
<attribute name="name" />
<attribute name="address1_line1" />
<attribute name="address1_line2" />
<attribute name="address1_line3" />
<filter type="and">
<condition attribute="name" operator="not-null" />
</filter>
</link-entity>
</link-entity>
</entity>
</fetch>
If your question really is "Is it possible to write a single FetchXML query that gets a SINGLE root entity and multiple children" then the answer is unfortunately no. However if you are able to handle duplicates of root data (For example using the Grouping functionality of an SSRS report) then what you require is entirely possible
Unless I've misunderstood the question this is very possible.
So for example you want to find all the contacts related to a given account. This is represented in Crm by the Parent Customer Lookup on the contact to the account.
<fetch mapping="logical" count="100" version="1.0">
<entity name="account">
<attribute name="name" />
<link-entity name="contact" from="parentcustomerid" to="accountid">
<attribute name="fullname" />
</link-entity>
</entity>
</fetch>
Which gives you a result set that looks like this:
<resultset morerecords="0" paging-cookie="<cookie page="1"><accountid last="{E704FAD6-2D4B-E111-9FED-00155D828444}" first="{AD912122-6B3C-E111-9B37-00155D828444}" /></cookie>">
<result>
<name>RGD Mining Inc</name>
<accountid>{E704FAD6-2D4B-E111-9FED-00155D828444}</accountid>
<accountid.fullname>Bill Miner</accountid.fullname>
</result>
<result>
<name>RGD Mining Inc</name>
<accountid>{E704FAD6-2D4B-E111-9FED-00155D828444}</accountid>
<accountid.fullname>Green</accountid.fullname>
</result>
</resultset>
No, it is not possible.
I'm happy to report that it is possible. I have a solution that worked well for me.
The only caveat is that if you have multiple child accounts you will get multiple results for the parent. For example:
parent 1: child 1
parent 2: child 1
parent 2: child 2
This means that you would then have to run the results through a sorting function, to get either all the children under parents in a multi dimensional array, or get all the accounts as unique entries in a flat array.
Also, this only goes down one level. If your hierarchy is multi-level, this code would need to be modified.
<fetch distinct="false" mapping="logical">
<entity name="account">
<attribute name="name" />
<link-entity name="account" alias="childaccount" to="accountid" from="parentaccountid" link-type="outer">
<attribute name="name" alias="childname" />
</link-entity>
</entity>
</fetch>

Resources