Advanced Find - combine related entitie with OR
dynamics-crm, dynamics-crm-2011
Solution
I solved the problem.
- Create Plugin for pre-validation with `Execute()` method and some method for data parsing.
- In entity view add some field with `GUID`.
- If plugin found guid in your view, it 'll get fetchxml query for your entity and second query entity else 'll show default view.
- Parse data for what you want show for user.
- Register your plugin.
- Profit.
PS I'll add sources in day or two after refactoring and approval from customer.
Edit:
First of all - y need create new `GUID` and add string field to view with that guid to view(It is better to hide it from the user). Create plugin with `RetrieveMultiple` action and `Post` validation(in `Pre` action you may lose your changes)
In plugin: main method `RetrieveMultiple` which will get context and service from wich you will take query, then you need get fetchXml and check if there are your `GUID`.
string fetchXml = string.Empty;
var query = context.InputParameters["Query"] as QueryExpression;
var fetchQuery = context.InputParameters["Query"] as FetchExpression;
if (query == null)
{
if (fetchQuery == null)
{
return;
}
fetchXml = fetchQuery.Query;
}
// Convert query to a fetch expression for processing and apply filter
else
{
fetchXml =
((QueryExpressionToFetchXmlResponse)
service.Execute(new QueryExpressionToFetchXmlRequest {Query = query})).FetchXml;
}
if (fetchXml.Contains(OpportunityFilterGuid))
{
ApplyFilter(context, service, query);
}
}
In your `ApllyFilter` method you need:
Get query from user(he can add some new fileds).
Delete your field with `GUID`.
Execute query.
Delete fileds, that can conflict with your `OR` statement.
Add `link-entity` to query.
Execute query.
Add received entities from second query to first.
Using LINQ select entities wich are not repeated.
collectionOne.Entities.GroupBy(oppId => oppId.Id).Select(opp => opp.First())
Send that data to client.
Problem
Is there any way to create that query? I need data from Adress and Contact Adress, normally i can just combine them by `Combine OR` but not in this case. I guess that i must write new plugin with `PreExecute()` method, get my query, parse data and then manualy get equal address OR there are other way?