Query CRM 2011 with Outer Join - dynamics-crm

I am using the code below to query the appointment set in CRM 2011. When I use this query it returns the data, but it returns duplicates. If I take out the Outer joins it doesn't return anything at all. Any ideas on how to fix this?
<fetch>
<entity name="appointment">
<attribute name="scheduledstart" />
<link-entity name="systemuser" from="systemuserid" to="ownerid" link-type="outer">
<attribute name="firstname" alias="ownerFirstName" />
<attribute name="lastname" alias="ownerLastName" />
</link-entity>
<link-entity name="contact" from="contactid" to="new_contactperson" link-type="outer">
<attribute name="parentcustomerid" alias="parentaccount" />
<attribute name="new_businessunit" alias="businessunit" />
</link-entity>
<attribute name="new_contactperson" />
<attribute name="subject" />
<attribute name="new_coldernotes" />
<link-entity name="activityparty" from="activityid" to="activityid" link-type="outer">
<attribute name="participationtypemask" alias="participationtypemask" />
<filter>
<condition attribute="participationtypemask" operator="eq" value="5" />
</filter>
<link-entity name="contact" from="contactid" to="partyid" link-type="outer">
<attribute name="fullname" alias="RequiredContactFullName" />
</link-entity>
<link-entity name="systemuser" from="systemuserid" to="partyid" link-type="outer">
<attribute name="fullname" alias="RequiredOwners" />
</link-entity>
<link-entity name="account" from="accountid" to="partyid" link-type="outer">
<attribute name="name" alias="RequiredAccount" />
</link-entity>
</link-entity>
<!--the new join-->
<link-entity name="activityparty" from="activityid" to="activityid" alias="optionalactivityparty" link-type="outer">
<attribute name="participationtypemask" alias="optionalparticipationtypemask" />
<filter>
<condition attribute="participationtypemask" operator="eq" value="6" />
</filter>
<link-entity name="contact" from="contactid" to="partyid" alias="optionalcontact" link-type="outer">
<attribute name="fullname" alias="OptionalContactFullName" />
</link-entity>
<link-entity name="systemuser" from="systemuserid" to="partyid" alias="systemuser2" link-type="outer">
<attribute name="fullname" alias="OptionalOwners" />
</link-entity>
<link-entity name="account" from="accountid" to="partyid" alias="optionalaccount" link-type="outer">
<attribute name="name" alias="OptionalAccount" />
</link-entity>
</link-entity>
<filter type="and">
<condition attribute="scheduledstart" operator="on-or-after" value="#FromDate" />
<condition attribute="scheduledstart" operator="on-or-before" value="#ToDate" />
</filter>
</entity>
</fetch>
Thanks!

Try indicating that you want a distinct record set by changing your root fetch node to
<fetch distinct='true'>
...
</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

Fetch XML SSRS Report

When i create a fetch xml ssrs report using the following fetch xml
<fetch distinct="true" >
<entity name="incident" >
<attribute name="ticketnumber" />
<attribute name="createdon" />
<attribute name="ssg_workedby" />
<attribute name="statuscode" />
<attribute name="ssg_trustid" />
<attribute name="statecode" />
<attribute name="ssg_requestraisedby" />
<attribute name="caseorigincode" />
<attribute name="title" />
<attribute name="createdby" />
<attribute name="ssg_turnaroundtime" />
<attribute name="ownerid" />
<attribute name="ssg_abouttofailfirstsla" />
<attribute name="ssg_abouttofailsecondsla" />
<attribute name="responseby" />
<attribute name="ssg_fixcode2cause" />
<attribute name="ssg_fixcode3owner" />
<attribute name="ssg_queue" />
<filter type="and" >
<condition attribute="createdon" operator="on-or-before" value="03-03-2020" />
<condition attribute="createdon" operator="on-or-after" value="01-01-2020" />
<filter type="or" >
<condition attribute="subjectidname" operator="eq" value="Complaint" />
<condition attribute="subjectidname" operator="eq" value="Failure in process" />
<condition attribute="subjectidname" operator="eq" value="Governance" />
<condition attribute="subjectidname" operator="eq" value="Time to resolve" />
</filter>
<filter type="or" >
<condition attribute="statecode" operator="eq" value="0" />
<condition attribute="statecode" operator="eq" value="1" />
</filter>
</filter>
<order attribute="createdon" />
<link-entity name="incidentresolution" from="incidentid" to="incidentid" link-type="outer" alias="CaseResolution" visible="true" >
<attribute name="actualend" />
<attribute name="subject" />
<filter type="and" >
<condition attribute="statuscode" operator="eq" value="2" />
</filter>
</link-entity>
</entity>
</fetch>
There are columns such as "statecode" which gets repeated 2 times. Please refer the screenshot for more information. Is there any way to reduce these two columns to only one?
This is because SSRS SSDT (Reporting Services) provides with State Code Value (0/1) and State Code (Active/Inactive).
In your report you are using both of them. You will need to remove any one depending on what you want.
Note: You do not need to change any thing in fetchxml, rather in Report builder just remove a column of Value or Name.

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>

fetchXml Dynamics 365 with sorting on link entity and paging working only for first page, not others

following query is working fine for first page
<fetch count="10" no-lock="true" page="1">
<entity name="account">
<attribute name="accountid" />
<attribute name="name" />
<link-entity name="contact" from="parentcustomerid" to="accountid" link-type="outer" alias="contact">
<attribute name="contactid" />
<attribute name="fullname" />
<attribute name="jobtitle" />
<attribute name="emailaddress1" />
<attribute name="telephone1" />
<attribute name="donotbulkemail" />
<attribute name="donotphone" />
<attribute name="telephone3" />
<attribute name="mobilephone" />
<attribute name="statuscode" />
<attribute name="statecode" />
<order attribute="lastname" />
</link-entity>
</entity>
</fetch>
the same query for second page gives only one record
<fetch count="10" no-lock="true" page="2">
<entity name="account">
<attribute name="accountid" />
<attribute name="name" />
<link-entity name="contact" from="parentcustomerid" to="accountid" link-type="outer" alias="contact">
<attribute name="contactid" />
<attribute name="fullname" />
<attribute name="jobtitle" />
<attribute name="emailaddress1" />
<attribute name="telephone1" />
<attribute name="donotbulkemail" />
<attribute name="donotphone" />
<attribute name="telephone3" />
<attribute name="mobilephone" />
<attribute name="statuscode" />
<attribute name="statecode" />
<order attribute="lastname" />
</link-entity>
</entity>
</fetch>
No idea, What is wrong with second and onward page request?
If I remove the order attribute from link entity then it is working fine.
Are you using a paging cookie? That will not work in this case, where there are multiple rows returned for each account.
When not to use paging cookies
Paging cookies depend on the common case where each row returned represents a unique entity record. There
are some queries you can construct using link-entity that will provide
rows that combine data from the primary entity with related entities.
This will result in multiple primary entity rows that refer to the
same primary key value. If you depend on paging cookies in this
situation you will get inconsistent results.
My preferred way to fix this issue is to "flip" your query around: Make contact your primary entity and link to account instead (presuming this would be a suitable query for you). Your query would then look as follows:
<fetch count="10" no-lock="true" page="1">
<entity name="contact">
<attribute name="contactid" />
<attribute name="fullname" />
<attribute name="jobtitle" />
<attribute name="emailaddress1" />
<attribute name="telephone1" />
<attribute name="donotbulkemail" />
<attribute name="donotphone" />
<attribute name="telephone3" />
<attribute name="mobilephone" />
<attribute name="statuscode" />
<attribute name="statecode" />
<order attribute="lastname" />
<link-entity name="account" from="accountid" to="parentcustomerid" link-type="outer" alias="account">
<attribute name="accountid" />
<attribute name="name" />
</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>

Resources