I have two (related) questions:
I am having a puzzling problem with the Advanced Find function. I set up the fields I require, both in the criteria and display section and then hit 'Download Fetchxml'. What I end up with is fields that I never asked for. For example, in my advanced find I asked for All Activities. I changed the results to show me only the Date Created, Activity Type, Subject and Regarding fields. The (truncated) generated fetchXML looks like this:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="activitypointer">
<attribute name="activitytypecode" />
<attribute name="subject" />
<attribute name="activityid" />
<attribute name="instancetypecode" />
<attribute name="community" />
<attribute name="createdon" />
<attribute name="regardingobjectid" />
<order attribute="subject" descending="false" />
Why have those extra columns been included? I didn't ask for the 'community' attribute anywhere, for example.
Second question:
What determines the output order of the fields? I ran the above fetchXML through Fetch Tester 3000 (saved my life - thank you!) and the output table bears no relation to the order of the attributes in the xml. This is true also when I use the fetchXML elsewhere.
Thanks in advance for your comments
The FetchXML returned by the button Download Fetch XML is generated internally and in some scenarios it returns extra columns (as you found), for example the primary key field of the entity is always added.
If you remove the additional attributes the FetchXML is still valid and can be executed.
Regarding the order, the tool you used (Fetch Tester 3000) display the fields inside the Table View ordered by their logicalname, inside Dynamics CRM the order of the attributes (subgrids and advanced find results) is defined using another XML definition known as LayoutXML
Related
I have an entity called as medicalcase
Each medical case has a subgrid, N:N relationship with an entity called as mcfamily
Each mcfamily has Father, Mother, Child fields which are lookups to contact field
Now if I make a webapi call
http://serverurl/api/data/v8.0/new_medicalcase(caseid)?$expand=new_medcase_mcfamily
I get the new_father_value, but I want to expand the Father lookup and get the contact name. How can I do it?
I tried
http://serverurl/api/data/v8.0/new_medicalcase(caseid)?$expand=new_medcase_mcfamily($expand=new_father($select=fullname))
But it said
navigation property can't be expanded. Multiple levels of expansion
aren't supported.
You'll have to manually expand by making a second request for the father contact.
If you make the request using a FetchXml query, you should be able to double-expand with one call. Here's an example of how to send a fetch query via the WebAPI (too long to copy here).
Update in response to your comment
Here's a fetch query that would retrieve all of your father names in one call. It assumes the following logical names:
medicalcase entity: new_medicalcase
mcfamily entity: new_mcfamily
lookup to father contact: new_fatherid
N:N entity: new_medicalcase_new_mcfamily
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
<entity name="contact" >
<attribute name="fullname" />
<link-entity name="new_mcfamily" from="new_fatherid" to="contactid">
<link-entity name="new_medicalcase_new_mcfamily" from="new_mcfamilyid" to="new_mcfamilyid" intersect="true">
<link-entity name="new_medicalcase" from="new_medicalcaseid" to="new_medicalcaseid">
<filter>
<condition attribute="new_medicalcaseid" operator="eq" value="FAEEE5D8-D67C-E511-80E6-3863BB3CA578" />
</filter>
</link-entity>
</link-entity>
</link-entity>
</entity>
</fetch>
To get the query working right, use the FetchXml Tester tool that comes with the XrmToolbox. After you get it working correctly, reference the link from earlier in my answer to execute the fetch query using the WebAPI.
I am using a service that gets data from CRM ONLINE and transfers it to SQL database for reporting purposes. I am using the following Fetch XML query to query CRM
string fetchXml = string.Format(#"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='new_studentinformation' enableprefiltering ='1' prefilterparametername='CRM_Filterednew_studentinformation'>
<attribute name='new_studentid' />
<attribute name='new_primaryhomeroom' />
<attribute name='new_lastname' />
<attribute name='new_firstname' />
<attribute name='new_busnumber' />
<attribute name='new_schoolname' />
<attribute name='new_gradecode' />
<attribute name='modifiedon' />
<attribute name='new_studentinformationid' />
<filter type='and'>
<condition attribute='modifiedon' value='{0}' operator='on-or-after'/>
</filter>
<order attribute='modifiedon' descending='true' />
<link-entity name='annotation' from='objectid' to='new_studentinformationid' alias='Notes' link-type='outer'>
<attribute name='documentbody'/>
<filter type='and'>
<condition attribute='createdon' value='{0}' operator='on-or-after'/>
</filter>
<order attribute='createdon' descending='true' />
</link-entity>
</entity>
</fetch>"
Every record has more than one image attached to it in Notes entity, however I want to transfer the latest picture ONLY. I have tried using the createdon in the order attribute but it keep bringing the images with the record till the oldest one. I only want it to bring the latest image and stop it there and move to the next record.
How can I limit it to querying only the latest image attached with the record?
Unfortunately <link-entity> doesn't allow it.
If you want a quick cheat around this limitation, you could do this (we do it all the time when specs go nuts):
Add a lookup to annotation to your new_studentinformation entity (keep it out of forms)
Register a plugin on new_studentinformation (Pre-Op, Sync, both for Retrieve and RetrieveMultiple) in which you fill the new attribute with the reference to the record you want in the report. I expect this to take 15 minutes tops.
Now switch the from attribute in the link-entity to the new one
BONUS: If your plugin is designed correctly, the reference in the custom attribute (and consequently your report) will automagically update itself.
I have a simple solution for your problem which will save you time and effort which you will put in making a plugin.
You should make a javascript resource , add a hidden field on your form.
Run that javascript on OnSave event. Store that document body in a text view.
Just query the document body text area while you are retrieving the image.
It may sound a hack! but it will increase the efficiency of your code.
Is it possible to find all accounts who has not modified any activity in last one month in CRM. I have tried one but i am not sure whether it is correct or not. can anybody please help me. I am not sure whether it is possible or not.
<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="activitypointer" from="regardingobjectid" to="accountid" alias="au" link-type="outer">
<filter type="and">
<condition attribute="modifiedon" operator="olderthan-x-months" value="1" />
</filter>
</link-entity>
I think the best way to do this without making multiple queries is to create a custom field on the account entity and then have a plugin that populates that field whenever an activity related to the account is updated. That will make your query a lot easier and work in CRM.
I'm afraid that it is not possible to get such kind of fetch xml. You will have to implement your logic in 2 steps:
Retrieve all accounts.
Remove all the accounts that have activities that were modified during last month.
I guess your problem here is that you will only get Accounts that have an associated activity. While your query also needs to return accounts that have no associated activities as well.
What you need is a Left Outer Join. This is only available in 2013 so, if you are upgrading any time soon, you will be able to add the following condition to your query to get the required results.
<condition entityname='account' attribute='regardingobjectid' operator='null'/>
I am having a bizarre problem, and was hoping that someone could shed some light on why it is occurring. If I Query dynamics using FetchXML using the following:
<fetch mapping="logical" count="10" distinct="true" version="1.0">
<entity name="activitypointer">
<attribute name="activityid" />
<attribute name="activitytypecode" />
<attribute name="createdon" />
<attribute name="ownerid" />
<attribute name="statecode" />
<attribute name="statuscode" />
<attribute name="subject" />
<attribute name="actualend" />
<order attribute="actualend" descending="true" />
<filter type="or">
<condition attribute="regardingobjectid" operator="eq" value="66431c2f-fab6-dd11-94f2-0014221f6f5c" />
</filter>
</entity>
Then I get the following set of results:
However if I do the same query in SQL:
SELECT TOP 100 activityid, activitytypecodename, createdon, owneridname, statecodename, statuscodename, subject, actualend FROM [CRM2011_MSCRM].[dbo].[FilteredActivityPointer] WHERE regardingobjectid = '66431C2F-FAB6-DD11-94F2-0014221F6F5C' ORDER BY actualend DESC
Then I get the following results:
You can see that the results are pretty much the same, however if you look at the status code column then you can see that the results differ in the fact that one states that it has been sent while the other has been completed.
Therefore how can I filter based upon the status of the item when the two different methods seem to bring back different results for the state code?
(I am currently trying to do a report in SSRS using FetchXML I have previously tried doing it in SQL but had problems, therefore used FetchXML to solve this)
-- Edit
When I use the appointment entity then I do get the correct results:
<fetch mapping="logical" count="50" version="1.0">
<entity name="appointment">
<attribute name="activityid" />
<attribute name="activitytypecode" />
<attribute name="actualend" />
<attribute name="createdon" />
<attribute name="ownerid" />
<attribute name="statecode" />
<attribute name="statuscode" />
<attribute name="subject" />
<filter>
<condition attribute="regardingobjectid" operator="eq" value="66431c2f-fab6-dd11-94f2-0014221f6f5c" />
</filter>
</entity>
Which returns this:
The only problem with this though is I need to know the status for a whole list of different activities - thus I thought you needed to use the activity pointer (as it is the base class plus you can't join results together in SSRS.)
Well, this isn't an answer on how to solve this, perhaps, but I think I can shed some light on why it may be happening.
For the statuscode of an activitypointer, fetchXML is taking the integer value and mapping it to the activitypointer lookup table, while SQL maps it to the actual child entity's table.
For example, take your very first record, which happens to be an email. FetchXML says the status is "cancelled". Checking the activitypointer metadata (http://msdn.microsoft.com/en-us/library/gg328148.aspx) we see that "cancelled" is value "3". Now, going to the email entity metadata (http://msdn.microsoft.com/en-us/library/hh155312.aspx) we see that value "3" is actually "sent", which is what SQL says.
This goes for all the entities you have in the table. The lonely Task with no fetchXML status, for example, is actually completed according to sql. That's value "5" (http://msdn.microsoft.com/en-us/library/gg594424.aspx), which doesn't map to anything for activitypointers; their values are limited to a range of 1-4. As a result, fetchXML shows nothing.
Try getting the statuscodeValue rather than name. I expect the integer values will be identical for both fetchXML and SQL. Why they map the names differently I can't answer.
I am trying to create a view that will display data from an order. There is a one to many relationship from the order to the payments for an order. The payments will display on the order form. The problem I'm having is that the information from the payment does not display when you run the query. Here is the XML from the view.
<?xml version="1.0"?>
-<fetch distinct="false" mapping="logical" output-format="xml-platform" version="1.0">
-<entity name="salesorder"><attribute name="name"/>
<attribute name="customerid"/>
<attribute name="totalamount"/>
<attribute name="salesorderid"/>
<order descending="false" attribute="name"/>
-<filter type="and"><condition attribute="totalamountlessfreight" value="0"
operator="gt"/>
<condition attribute="ree_orderdate" operator="this-month"/>
</filter>
-<link-entity name="ree_salesorderpayment" alias="a_eaddfe488ba0e2118a9d78e3b508542d" link- type="outer" visible="false" to="ree_payment" from="ree_salesorderpaymentid">
<attribute name="ree_paymentdate"/>
<attribute name="ree_paymentamount"/>
</link-entity>
</entity></fetch>
Is this possible?
Thanks,
Gary
The Stunnware Tools app is old but still good for FetchXML. One thing I've noticed before ref strange behaviour with queries is that if there are currency fields behaving oddly, then adding the transactioncurrencyid field to the columns can help.
Did you create this XML? One cool new thing is the Advanced Find you can download the FecthXml. And in Advanced Find you can change the filters and see where your query is failing.
EDIT: In Advanced find you can set all query you want, make filters (just add columns and apply the operators AND and OR) and you can make joins when you want filter a related entity. After that you can download the FetchXML, so we have sure that FetchXML is well formatted. If don't get any records the problem is in your data and not in FetchXML, so i advised you remove the filters to know where are the problems.
I don't know which login you are but if you aren't using an administrator it's possible you catch something about security permissions.
You can try change the entities, make the query to ree_salesorderpayment entity and after make a join with salesorder.