Reputation: 10669
I need to use the Mircosoft CRM SDK to pull a group of addresses based on the parentid
stored in each of the address entities. This parentid
naturally corresponds to the GUID
of the parent record.
I've built this query two ways: first I simply place the GUID
in the condition.Values
property, and second I tried using and EntityReference
.
If I simply use the GUID
the query completes but I receive no results. If I use the EntityReference
I get the following exception:
There was an error while trying to serialize parameter http://schemas.microsoft.com/xrm/2011/Contracts/Services:query. The InnerException message was 'Type 'Microsoft.Xrm.Sdk.EntityReference' with data contract name 'EntityReference:http://schemas.microsoft.com/xrm/2011/Contracts' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types
I've also tried building the query using two different methods, however I get the same results along the same lines of GUID
input into the condition value.
If I open the Microsoft Dynamics web page for the account I'm working on, I can clearly see that the address has been entered.
How do you go about including GUID
to query on the parentid
?
Method One
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "parentid";
condition.Operator = ConditionOperator.Equal;
//This works but returns no results
condition.Values.Add(new EntityReference("account", id));
//This throws the above exception
//condition.Values.Add(new EntityReference("account", id));
ColumnSet column = new ColumnSet(true);
QueryExpression query = new QueryExpression();
query.ColumnSet = column;
query.EntityName = "customeraddress";
query.Criteria.AddCondition(condition);
Method Two
QueryExpression queryEx = new QueryExpression
{
EntityName = "customeraddress",
ColumnSet = new ColumnSet(true),
Criteria =
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "parentid",
Operator = ConditionOperator.Equal,
//Works but returns no results
Values = {id}
//Throws exception
//Values = {new EntityReference("account", id)}
}
}
}
};
Upvotes: 2
Views: 10847
Reputation: 3143
One other way to fetch this data is by Downloading the FetchXML of that Query from Advanced Find then executing that Fetch Expression and adding the Id as a string :
string fetchxml= @" <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
<entity name="customeraddress">
<attribute name="name" />
<attribute name="line1" />
<attribute name="city" />
<attribute name="postalcode" />
<attribute name="telephone1" />
<attribute name="customeraddressid" />
<order attribute="name" descending="false" />
<link-entity name="account" from="accountid" to="parentid" alias="aa">
<filter type="and">
<condition attribute="accountid" operator="eq" uitype="account" value="{7EDECF7F-AE71-E511-80EC-3863BB3610E8}" />
</filter>
</link-entity>
</entity>
</fetch>";
FetchExpression fetch = new FetchExpression(fetchxml);
EntityCollection fetchresults = service.RetrieveMultiple(fetch);
You can then replace the Guid value in the fetchxml string with your guid string.
Upvotes: 0
Reputation: 1453
Query addresses in advance find:
If this results some records then below simple query should have data for u.
private EntityCollection getAddresses(Guid AccountID, IOrganizationService service)
{
QueryExpression query = new QueryExpression("customeraddress");
query.ColumnSet = new ColumnSet(true);
query.Criteria.AddCondition("parentid", ConditionOperator.Equal, AccountID);
EntityCollection results = service.RetrieveMultiple(query);
return results;
}
Upvotes: 1