Limit the result from FetchXML query in CRM online 2015 - dynamics-crm-2011

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.

Related

Report on accounts with no open activities in CRM 2015

I am working on creation of report for "No open activities on Accounts of type Phone call" in advance find. I am able to get list of accounts with completed activities of type phone call but not accounts with no activities at all.
I tried assigning Activity type and subject to does not contain data, it still does not fetch accounts with no activities.
In below picture, you will see filters I have used,
No Open Activity image
You should try using query like following:
<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="activityparty" from="partyid" to="accountid" alias="ae" link-type="outer">
<link-entity name="activitypointer" from="activityid" to="activityid" alias="af" link-type="outer">
<filter type="and">
<condition attribute="activitytypecode" operator="eq" value="4210" />
</filter>
</link-entity>
</link-entity>
<filter type="and">
<condition entityname="af" attribute="activityid" operator="null" />
</filter>
</entity>
</fetch>
Code is based on following article - https://msdn.microsoft.com/en-us/library/dn531006.aspx
Left outer joins aren't supported in Advanced Find. You will have to use a custom query like the one Andrii posted.
EDIT: If you want to run your report with custom fetchXml maybe using the Reporting Authoring extensions for SQL Server Data tools. You could copy & paste your fetchXml there and then upload the report into CRM.

Dynamics CRM 2013: Advanced Find FetchXML contains extra fields - why?

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

MS CRM 2011: How to get the associated entity-name for a privilege

I'm retrieving a list of all privileges from CRM:
QueryExpression q = new QueryExpression("privilege") {ColumnSet = new ColumnSet(true)};
var list = service.RetrieveMultiple(q).Entities;
This works fine.
I would now like to know the entity-name every privilege applies to. I.e. the privilege "prvDeleteNote" applies to the entity "Note".
This is no problem querying the database with SQL directly, but I would much prefer to retrieve the information from a service.
Cheers
As far as I know privilege entity doesn't have a property to identity the related entity.
As it turns out, this FetchXML returns what I was looking for:
<fetch mapping=""logical"" version=""1.0"">
<entity name=""privilege"">
<attribute name=""privilegeid"" />
<attribute name=""name"" />
<attribute name=""canbelocal"" />
<attribute name=""canbedeep"" />
<attribute name=""canbeglobal"" />
<attribute name=""canbebasic"" />
<link-entity name=""privilegeobjecttypecodes"" from=""privilegeid"" to=""privilegeid"" link-type=""outer"" alias=""otc"">
<attribute name=""objecttypecode"" />
</link-entity>
</entity>
</fetch>
I can access the Type with:
((AliasedValue)privilegeEntity.Attributes["otc.objecttypecode"]).Value.ToString()
where privilegeEntity is one entity from the response.
I was expecting the int value ObjectTypeCode, and was suprised that it returns the name of the entity.

How Can I Find All Accounts Who Have Not Modified Any Activity in Last One Month Using FetchXMl

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'/>

Fetch XML vs SQL CRM2011

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.

Resources