MS CRM Online: Get all accounts with more than X contacts - dynamics-crm

Is it possible to get all accounts in MS CRM Online with more than X contacts via odata (this is probably not possible via FetchXML)?
Something like (dummy code, does not work):
accounts?$apply=aggregate(contact_customer_accounts with countdistinct as total)/filter(total gt X)

Unfortunately, this is not possible as it is not straight forward with a single query using fetchxml (no support for subquery and aggregation with having clause) or web api. You can pull all the results and do aggregation using LINQ or other ways.
Compared to that, using a single rollup attribute to count the child contact records on parent account (not a calculated field) is the easiest option.

Related

Filtering across multiple Dynamics entities in Common Data Service

I'm trying to query our Dynamics 365 CRM through the Common Data Service / Microsoft Dataverse. I can manage this when I need to just filter on fields that exist within the entity type I'm searching. But I also need to filter on a different linked entity.
So far I can query on the contact entity just fine:
var contacts = svc.Get("contacts?" +
"$select=firstname,lastname,address1_name,address1_line1," +
"address1_postalcode,emailaddress1,statecode,_parentcustomerid_value&" +
$"$filter=contains(firstname,'{firstName}') and " +
$"contains(lastname, '{lastName}')"
, formattedValueHeaders);
And this returns a list of contacts which match the filter.
However, I also need to filter on the Account Name that the contact is linked to. If I had the AccountId value then I could do this by adding $"contains(_parentcustomerid_value, '{accountId}')" to the filter. But I can't use this as the input data I'm receiving to query on is the name of the account and not an Id value.
Any ideas on how to do this?
Currently, the only way I can see is to use my existing query, then to loop through each contact and check the returned account name in the response, and if this matches then in effect I've filtered it. But I would prefer to be able to have the whole filter all in a single query.
You can use this to filter based on lookup field name value.
https://devorg.crm.dynamics.com/api/data/v9.1/contacts?$filter=parentcustomerid_account/name eq 'test'

How to show records related to upper records too in a subgrid?

How can I build a subgrid that displays related records both of the current record and records above it, and can it contain complex conditions ?
One of my requirements is this:
I want to put a subgrid in Account form showing the related Contacts of the current Account, and also include the Contacts related to Accounts above the current one if a Two Options attribute of the contact is set to Yes.
Specifically, I'm looking at the Company Name on Contact to establish it's related to an Account.
I'm at a loss and start to doubt it's doable.
Not an out of the box solution but if you don’t mind losing the sub grid command bar functionality you can use the following concept that displays a fetchxml inside a sub-grid.
Step 1 - would be to get all related accounts ids using a fetch or odata
Step 2 - construct a fetchxml with OR filter to get all contacts

How to retrieve all records for an entity in Dynamics CRM 4.0?

Is there a way to retrieve all of the records for a given entity using the Microsoft Dynamics CRM 4.0 SDK? I want the equivalent of:
SELECT foo
FROM new_bar
... where new_bar is the name of the entity.
This seems trivial, but I can't find any examples on how to retrieve all of the records for a given entity.
Because you want to retrieve all the records for a given entity, you need to used the Paging Cookie, otherwise you will get maximum 5000 records.
You can find an example here:
http://msdn.microsoft.com/en-us/library/cc151070.aspx

web api odata service - return complex type

i want to create web api odata service that return reault type which consist of collection data member and additional members like this service return:
http://services.odata.org/OData/OData.svc/Suppliers?$filter=Address/City eq 'Redmond'
as you can see the result type is consist of collection data member and additional members
can anyone send me a sample how to do it?
i can't succeed to create this kind of complex type and to be able filter the collection items by there values
as yuo can see in this query it return all the result without filter the items
services.odata.org/OData/OData.svc/Suppliers
i want to be able filter this type like this:
services.odata.org/OData/OData.svc/Suppliers?$filter=Address/City eq 'Redmond'
in this query i managed to filter the collection member items and still returning the other data members.
If you just want to implement filters like: services.odata.org/OData/OData.svc/Suppliers?$filter=Address/City eq 'Redmond'
Please check the sample at http://aspnet.codeplex.com/SourceControl/changeset/view/903afc4e11df#Samples%2fNet4%2fCS%2fWebApi%2fODataServiceSample%2fReadMe.txt
It has a supplier and address model with queryable attribute. It should work with the same $filter query.
Odata support was implicit in asp.net webapi RC.
You just had to return a IQueryable from you Actions and mark it with [QueryableAttribute].
Only this much supported querystring based data filtering.
I was a bit disappointed when I saw the [QueryableAttribute] doesnt build in RTM.
In RTM it’s available as a separate package, Microsoft.AspNet.WebApi.OData on Nuget in a preview/alpha form. Full release is coming later this fall. You can grab it from here(http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData). There is a nice overview post available (http://blogs.msdn.com/b/alexj/archive/2012/08/15/odata-support-in-asp-net-web-api.aspx)

Collection Exists Criteria in WCF Data Services

I'm trying to ask my OData service:
"Give me all the Products that do not have a Category"
with the Products and Category have a m2m relationship.
I've tried:
from p in Products
where p.Categories == null
select p
and
from p in Products
where !p.Categories.Any()
select p
and
from p in Products
where p.Categories.Count == 0
select p
but all of these give me not supported exceptions.
I'm not looking for alternatives or options. Please don't answer with other options.
My experience with WCF Data Services is that the client subset of LINQ to rest is lacking.
My biased option would be to just move it to server side where you have access to the full implementation of LINQ to entites? (or whatever you are using to implement your Data Service).
[WebGet]
public IQueryable<Products> GetProductsWithoutCategories(){
/*start psudo code
from p in Products
where p.Categories.Count == 0
select p
*/
}
This is not supported:
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/b505d630-c808-4bde-b08e-3ce1dd17f621/
The OData URL query language currently
doesn't support this type of query. As
a result the client's LINQ processor
doesn't support it either. If you
think it's valuable to add such
functionality please use our connect
site to suggest the feature, it makes
our planning job easier next time
around.
https://connect.microsoft.com/dataplatform/content/content.aspx?ContentID=15540&wa=wsignin1.0
As a workaround you could probably use
service operation. Define a service
operation which returns IQueryable (so
that you can compose some more query
operators on the result from the
client) and use the server side
provider to issues the query above.
My solution to this problem was to $expand the related field in the query, and then test if that field was empty or not...
JArray _array = (JArray)main_table_object["some_related_field"];
if (_array.Count > 0)
continue;
.. this is in the context of queries from Windows 7 phone to a WCF service using JSON as the message format.

Resources