FetchXML: get count of records based on a field of a linked entity - entities

I have a scenario where i want to get the count of people from an Entity who have multiple location address marked as current address in other linked entity.
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" aggregate="true">
<entity name="client">
<attribute name="clientid"/>
<attribute name="name"/>
<attribute name="createdon"/>
<order attribute="name" descending="false"/>
<link-entity name="locationadd" from="clientid" to="clientid" alias="aa">
<attribute name="locationadd" aggregate="countcolumn" />
<filter type="and">
<condition attribute="isthiscurrentadd" operator="eq" value="1"/>
</filter>
</link-entity>
</entity>
</fetch>

The answer is
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' aggregate='true'>
<entity name='client'>
<attribute name='clientid' alias='PersonName' groupby='true' />
<link-entity name='locationadd' from='clientid' to='clientid' alias='aa'>
<attribute name='isthiscurrentadd' alias='Count' aggregate='count'/>
<filter type='and'>
<condition attribute='isthiscurrentadd' operator='eq' value='1'/>
</filter>
</link-entity>
</entity>
</fetch>

Try following - this code will return you the number of clients.
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" aggregate="true">
<entity name="client">
<attribute name="clientid" aggregate='count' alias='clientscount'/>
<link-entity name="locationadd" from="clientid" to="clientid" alias="aa">
<attribute name="locationadd" aggregate="countcolumn" />
<filter type="and">
<condition attribute="isthiscurrentadd" operator="eq" value="1"/>
</filter>
</link-entity>
</entity>
</fetch>

Related

FetchXML link entity including null values

I'm trying to query CRM using FetchXML. Here is the query to account entity, this returns me 10 records, here new_primaryactivityname field has NULL for few records.
<fetch mapping="logical" version="1.0">
<entity name="account">
<attribute name="accountid" />
<attribute name="name" />
<attribute name="new_primaryactivity" />
</entity>
</fetch>
Now i want to link it with another entity to get a new field, i want to do LEFT OUTER JOIN like in sql, to keep the NULL values while linking, so i provided link type as outer and i applied a filter for few conditions.
<fetch mapping="logical" version="1.0">
<entity name="account">
<attribute name="accountid" />
<attribute name="name" />
<attribute name="new_primaryactivity" />
<link-entity name="stringmap" from="attributevalue" to="new_primaryactivity" alias="new_primaryactivity_lookup_name" link-type="outer">
<attribute name="value" />
</link-entity>
<filter type="and">
<condition entityname="new_primaryactivity_lookup_name" attribute="attributename" operator="eq" value="new_primaryactivity" />
<condition entityname="new_primaryactivity_lookup_name" attribute="objecttypecode" operator="eq" value="999" />
</filter>
</entity>
</fetch>
The result(less than 10 records) ignores the records with new_primaryactivityname as NULL, what am i missing in FetchXML query?
I do not have much access to to system other than use FetchXML, i referred few suggestions to use tool in help building FetchXML query but i can't do it due to restrictions.
I think it's the location of your filter block.
The way it is written now, you're linking account and stringmap together with the outer-join, and then you're applying the filter to the results.
I think you need to do the filter inside the outer-join like so:
<fetch mapping="logical" version="1.0">
<entity name="account">
<attribute name="accountid" />
<attribute name="name" />
<attribute name="new_primaryactivity" />
<link-entity name="stringmap" from="attributevalue" to="new_primaryactivity" alias="new_primaryactivity_lookup_name" link-type="outer">
<attribute name="value" />
<filter type="and">
<condition attribute="attributename" operator="eq" value="new_primaryactivity" />
<condition attribute="objecttypecode" operator="eq" value="999" />
</filter>
</link-entity>
</entity>
</fetch>
Alternatively you could try to write the filter differently and explicitly allow for the null values
<fetch mapping="logical" version="1.0">
<entity name="account">
<attribute name="accountid" />
<attribute name="name" />
<attribute name="new_primaryactivity" />
<link-entity name="stringmap" from="attributevalue" to="new_primaryactivity" alias="new_primaryactivity_lookup_name" link-type="outer">
<attribute name="value" />
</link-entity>
<filter type="or">
<condition entityname="new_primaryactivity_lookup_name" attribute="attributename" operator="eq" value="" />
<filter type="and">
<condition entityname="new_primaryactivity_lookup_name" attribute="attributename" operator="eq" value="new_primaryactivity" />
<condition entityname="new_primaryactivity_lookup_name" attribute="objecttypecode" operator="eq" value="999" />
</filter>
</filter>
</entity>
</fetch>
I haven't tested either of these, but hopefully one of these works for you

FetchXML query to return all records owned by subordinates of current user

I have a org that uses Managerial hierarchy to organize Employees (Users). They'd like a view for managers. This view should return all Open Opportunities owned by Users who are managed by the current user, OR managed by someone who is managed by the current user, OR managed by someone who is managed by someone who is managed by the current user. These 3 levels is are all that we need.
I've already developed a FetchXML query that returns all Projects owned by Users who are managed by the current user.
<entity name="opportunity">
<attribute name="name" />
<attribute name="estimatedvalue" />
<attribute name="statuscode" />
<attribute name="parentcontactid" />
<attribute name="parentaccountid" />
<attribute name="opportunityid" />
<order attribute="name" descending="false" />
<filter type="and">
<condition attribute="statecode" operator="eq" value="0" />
</filter>
<link-entity name="systemuser" from="systemuserid" to="owninguser" link-type="inner" alias="ad">
<filter type="and">
<condition attribute="parentsystemuserid" operator="eq-userid" />
</filter>
</link-entity>
</entity>
</fetch>
Below is an alternative that also doesn't work. This returns all Opportunities that are owned by someone who is managed by current user, which manages them self.
<entity name="opportunity">
<attribute name="name" />
<attribute name="estimatedvalue" />
<attribute name="statuscode" />
<attribute name="parentcontactid" />
<attribute name="parentaccountid" />
<attribute name="opportunityid" />
<order attribute="name" descending="false" />
<filter type="and">
<condition attribute="statecode" operator="eq" value="0" />
</filter>
<link-entity name="systemuser" from="systemuserid" to="owninguser" link-type="inner" alias="aa">
<filter type="and">
<condition attribute="parentsystemuserid" operator="eq-userid" />
</filter>
<link-entity name="systemuser" from="systemuserid" to="parentsystemuserid" link-type="inner" alias="ab">
<filter type="and">
<condition attribute="parentsystemuserid" operator="eq-userid" />
</filter>
<link-entity name="systemuser" from="systemuserid" to="parentsystemuserid" link-type="inner" alias="ac">
<filter type="and">
<condition attribute="parentsystemuserid" operator="eq-userid" />
</filter>
</link-entity>
</link-entity>
</link-entity>
</entity>
</fetch>
You might be interested in the following owner operators:
eq-useroruserhierarchy
eq-useroruserhierarchyandteams
eq-useroruserteams
eq-userteams
From your description you might want eq-useroruserhierarchy
You use it like this:
<fetch top="50" >
<entity name="opportunity" >
<all-attributes/>
<filter>
<condition attribute="ownerid" operator="eq-useroruserhierarchy" />
</filter>
</entity>
</fetch>

Left Join fetchXml based on common column

I have two tables that are not related, but have a common column. I want to join the rows in table B that match with <attribute name='substitutedproductid'> in Table A. Table A looks like this:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="productsubstitute">
<attribute name='substitutedproductid' />
<attribute name='new_quantity' />
<attribute name='new_unit' />
<attribute name='new_grouping' />
<filter type="and">
<condition attribute="productid" operator="eq" value="{guid}" />
</filter>
</entity>
</fetch>
Table B looks like this:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="new_contractlinedislike">
<attribute name="new_contractlinedislikeid" />
<attribute name="substituteproductid" />
<attribute name="new_unit" />
<attribute name="new_quantity />
<attribute name="new_grouping" />
<filter type="and">
<condition attribute="new_contractlineid" operator="eq"  value="{guid}" />
</filter>
</entity>
</fetch>
As shown above, both tables have a lookup (substituteproductid), which can be to the same product. In the case that the lookups are the same, I want to do a left join onto Table A. Basically, I want to return a single entitycollection from a single fetchXml string. How can I achieve this?
Basically you have to use link-entity. This query should work for your scenario. Referred the old discussion.
<fetch version='1.0' output-format='xml-platform' mapping='logical' >
<entity name= 'productsubstitute' >
<attribute name='new_quantity' />
<attribute name='new_unit' />
<attribute name='new_grouping' />
<filter type='and' >
<condition attribute='productid' operator='eq' value= '{guid}' />
</filter>
<link-entity name='new_contractlinedislike' from='substitutedproductid' to='substituteproductid' link-type='outer' >
<attribute name='new_contractlinedislikeid' />
<attribute name= 'new_grouping' />
<filter type='and' >
<condition attribute='new_contractlineid' operator='eq' value= '{guid}' />
</filter>
</link-entity>
</entity>
</fetch>

MS CRM: How can I query for phone calls without Recipient ("Call To")

Wie imported a bunch of phoneCalls via Excel in our MS CRM 2013 system but for some of them the recipient (Call To) field were not set. Now we want to query for those records via FetchXml. How can we do this?
I tried the following, but it I received a lot of Phone Calls with recipient. Can you help?
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >
<entity name="activitypointer" >
<attribute name="activitytypecode" />
<attribute name="subject" />
<attribute name="statecode" />
<attribute name="instancetypecode" />
<attribute name="createdon" />
<attribute name="regardingobjectid" />
<attribute name="ownerid" />
<attribute name="activityid" />
<attribute name="prioritycode" />
<attribute name="scheduledend" />
<attribute name="createdby" />
<order attribute="createdon" descending="true" />
<order attribute="activitytypecode" descending="false" />
<filter type="and" >
<condition attribute="modifiedby" operator="eq-userid" />
<condition attribute="activitytypecode" operator="eq" value="4210" />
</filter>
<link-entity name="systemuser" from="systemuserid" to="owninguser" alias="activitypointerowningusersystemusersystemuserid" >
<attribute name="internalemailaddress" />
</link-entity>
<link-entity name="activityparty" from="activityid" to="activityid" link-type="outer" alias="ab" >
<filter type="and" >
<condition attribute="activitypartyid" operator="null" />
<condition attribute="participationtypemask" operator="eq" value="2" />
</filter>
</link-entity>
</entity>
</fetch>
I think you should try.
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
<entity name="phonecall">
<attribute name="subject" />
<attribute name="statecode" />
<attribute name="prioritycode" />
<attribute name="scheduledend" />
<attribute name="createdby" />
<attribute name="regardingobjectid" />
<attribute name="activityid" />
<order attribute="subject" descending="false" />
<link-entity name="activityparty" from="activityid" to="activityid" alias="ab">
<filter type="and">
<condition attribute="participationtypemask" operator="eq" value="2" />
<condition attribute="partyid" operator="null" />
</filter>
</link-entity>
</entity>
</fetch>

Join/Filter syntax for activity pointers using fetchxml

I have a requirement to pass in a fetch statement for a subgrid to use in CRM 2013. It needs to get the following:
All Activities regarding contacts who are connected to a contact through a connection. (This statement gets those)
<?xml version="1.0" encoding="UTF-8"?>
<fetch distinct="true" mapping="logical" output-format="xml-platform" version="1.0">
<entity name="activitypointer">
<attribute name="activitytypecode" />
<attribute name="subject" />
<attribute name="statecode" />
<attribute name="modifiedon" />
<attribute name="instancetypecode" />
<attribute name="scheduledend" />
<attribute name="actualend" />
<attribute name="regardingobjectid" />
<attribute name="activityid" />
<order descending="false" attribute="modifiedon" />
<link-entity name="contact" to="regardingobjectid" from="contactid">
<link-entity name="connection" to="contactid" from="record1id">
<filter type="and">
<condition attribute="record2id" value="someID" uitype="contact" operator="eq" />
</filter>
</link-entity>
</link-entity>
</entity>
</fetch>
All activities regarding opportunities related to the contact. ((This statement gets those))
<?xml version="1.0" encoding="UTF-8"?>
<fetch distinct="false" mapping="logical" output-format="xml-platform" version="1.0">
<entity name="activitypointer">
<attribute name="activitytypecode" />
<attribute name="subject" />
<attribute name="statecode" />
<attribute name="modifiedon" />
<attribute name="instancetypecode" />
<attribute name="scheduledend" />
<attribute name="actualend" />
<attribute name="regardingobjectid" />
<attribute name="activityid" />
<order descending="false" attribute="modifiedon" />
<link-entity name="opportunity" to="regardingobjectid" from="opportunityid">
<filter type="and">
<condition attribute="customerid" value="someID" uitype="contact" operator="eq" />
</filter>
</link-entity>
</entity>
</fetch>
And all activities regarding the contact itself.
I've tried a few different combinations of joins, but I'm not sure I'm getting their positioning or syntax correct. I unfortunately deleted those attempts after they all returned no results. Here is one of the last version I tried which also returned no results.
<?xml version="1.0" encoding="UTF-8"?>
<fetch distinct="false" mapping="logical" output-format="xml-platform" version="1.0">
<entity name="activitypointer">
<attribute name="activitytypecode" />
<attribute name="subject" />
<attribute name="statecode" />
<attribute name="modifiedon" />
<attribute name="instancetypecode" />
<attribute name="scheduledend" />
<attribute name="actualend" />
<attribute name="regardingobjectid" />
<attribute name="activityid" />
<order descending="false" attribute="modifiedon" />
<link-entity name="opportunity" to="regardingobjectid" from="opportunityid">
<attribute name="name" />
</link-entity>
<link-entity name="contact" to="regardingobjectid" from="contactid">
<link-entity name="connection" to="contactid" from="record1id">
<attribute name="record1id" />
</link-entity>
</link-entity>
<filter type="or">
<condition entityname="connection" attribute="record2id" value="" + Xrm.Page.data.entity.getId() + "" uitype="contact" operator="eq" />
<condition entityname="activitypointer" attribute="regardingobjectid" value="" + Xrm.Page.data.entity.getId() + "" uitype="contact" operator="eq" />
<condition entityname="opportunity" attribute="customerid" value="" + Xrm.Page.data.entity.getId() + "" uitype="contact" operator="eq" />
</filter>
</entity>
</fetch>
If anyone can help me out with formatting I'd appreciate it.
Thanks in advance.
It appears to me that although you can now use an OR filter across tables in CRM 2013 (your syntax looks correct) the OR does not apply to the link itself. In SQL terms it is as if you did a full join between the tables and then put the OR logic within the WHERE statement. If any of the joins doesn't match you will return no results.
select *
from activitypointer
join opportunity on ..
join connection on...
join contact on...
WHERE (opportunity.regardingID = SomeID) OR (opportunity.customerid = SomeID) OR (activitypointer.regardingobjectid = SomeID)
In your bottom example your activity must linked BOTH to an opportunity AND to a contact through a connection and one OR the other must meet your criteria within the OR filter. Unfortunately I do not think you can write the fetch you desire but I am not 100% sure of this so if anyone can confirm or refute this please do. This link has a good example of what you can do using OR filters across tables.

Resources