Is it possible to return the EntityCollection and include related records?
For instance, I have this code:
EntityCollection acclist = new EntityCollection();
acclist.Entities.Add(new Entity(Account.EntityLogicalName) {
["accountid"] = erf.Id, ["fax"] = 123
});
acclist.Entities.Add(new Entity(Account.EntityLogicalName) {
["accountid"] = new Guid("9f31bb4c-9125-ee11-9cbc-000d3ab1669a"), ["fax"] = 321
});
acclist.Entities.Add(new Entity(Account.EntityLogicalName) {
["accountid"] = new Guid("a131bb4c-9125-ee11-9cbc-000d3ab1669a"), ["fax"] = 456
});
acclist.Entities.Add(new Entity(Account.EntityLogicalName) {
["accountid"] = new Guid("a331bb4c-9125-ee11-9cbc-000d3ab1669a"), ["fax"] = 654
});
EntityCollection acclist2 = new EntityCollection();
acclist2.Entities.Add(new Entity(Account.EntityLogicalName) {
["accountid"] = erf.Id,
["fax"] = 777,
["_parentaccountid_value"] = new Guid("a331bb4c-9125-ee11-9cbc-000d3ab1669a"),
["_parentaccountid_value@Microsoft.Dynamics.CRM.associatednavigationproperty"] = "parentaccountid",
["_parentaccountid_value@Microsoft.Dynamics.CRM.lookuplogicalname"] = "account",
["_parentaccountid_value"] = "9f31bb4c-9125-ee11-9cbc-000d3ab1669a"
});
acclist.Entities.Add(new Entity(Account.EntityLogicalName) {
["accountid"] = new Guid("a331bb4c-9125-ee11-9cbc-000d3ab1669a"), ["fax"] = 666, ["account_parent_account"] = acclist2
});
string debug = JsonConvert.SerializeObject(acclist);
acclist.Entities.Add(new Entity(Account.EntityLogicalName) {
["accountid"] = new Guid("a331bb4c-9125-ee11-9cbc-000d3ab1669a"), ["fax"] = debug
});
context.OutputParameters["resultArray"] = acclist;
and as a result I get this:
"resultArray": [
{
"@odata.type": "#Microsoft.Dynamics.CRM.account",
"accountid": "d6208772-d545-eb11-a813-0022481eae24",
"fax": 123
},
{
"@odata.type": "#Microsoft.Dynamics.CRM.account",
"accountid": "9f31bb4c-9125-ee11-9cbc-000d3ab1669a",
"fax": 321
},
{
"@odata.type": "#Microsoft.Dynamics.CRM.account",
"accountid": "a131bb4c-9125-ee11-9cbc-000d3ab1669a",
"fax": 456
},
{
"@odata.type": "#Microsoft.Dynamics.CRM.account",
"accountid": "a331bb4c-9125-ee11-9cbc-000d3ab1669a",
"fax": 654
},
{
"@odata.type": "#Microsoft.Dynamics.CRM.account",
"accountid": "a331bb4c-9125-ee11-9cbc-000d3ab1669a",
"fax": 666
},
{
"@odata.type": "#Microsoft.Dynamics.CRM.account",
"accountid": "a331bb4c-9125-ee11-9cbc-000d3ab1669a",
"fax": "{\"Entities\":[{\"LogicalName\":\"account\",\"Id\":\"00000000-0000-0000-0000-000000000000\",\"Attributes\":[{\"Key\":\"accountid\",\"Value\":\"d6208772-d545-eb11-a813-0022481eae24\"},{\"Key\":\"fax\",\"Value\":123}],\"EntityState\":null,\"FormattedValues\":[],\"RelatedEntities\":[],\"RowVersion\":null,\"KeyAttributes\":[]},{\"LogicalName\":\"account\",\"Id\":\"00000000-0000-0000-0000-000000000000\",\"Attributes\":[{\"Key\":\"accountid\",\"Value\":\"9f31bb4c-9125-ee11-9cbc-000d3ab1669a\"},{\"Key\":\"fax\",\"Value\":321}],\"EntityState\":null,\"FormattedValues\":[],\"RelatedEntities\":[],\"RowVersion\":null,\"KeyAttributes\":[]},{\"LogicalName\":\"account\",\"Id\":\"00000000-0000-0000-0000-000000000000\",\"Attributes\":[{\"Key\":\"accountid\",\"Value\":\"a131bb4c-9125-ee11-9cbc-000d3ab1669a\"},{\"Key\":\"fax\",\"Value\":456}],\"EntityState\":null,\"FormattedValues\":[],\"RelatedEntities\":[],\"RowVersion\":null,\"KeyAttributes\":[]},{\"LogicalName\":\"account\",\"Id\":\"00000000-0000-0000-0000-000000000000\",\"Attributes\":[{\"Key\":\"accountid\",\"Value\":\"a331bb4c-9125-ee11-9cbc-000d3ab1669a\"},{\"Key\":\"fax\",\"Value\":654}],\"EntityState\":null,\"FormattedValues\":[],\"RelatedEntities\":[],\"RowVersion\":null,\"KeyAttributes\":[]},{\"LogicalName\":\"account\",\"Id\":\"00000000-0000-0000-0000-000000000000\",\"Attributes\":[{\"Key\":\"accountid\",\"Value\":\"a331bb4c-9125-ee11-9cbc-000d3ab1669a\"},{\"Key\":\"fax\",\"Value\":666},{\"Key\":\"account_parent_account\",\"Value\":{\"Entities\":[{\"LogicalName\":\"account\",\"Id\":\"00000000-0000-0000-0000-000000000000\",\"Attributes\":[{\"Key\":\"accountid\",\"Value\":\"d6208772-d545-eb11-a813-0022481eae24\"},{\"Key\":\"fax\",\"Value\":777},{\"Key\":\"_parentaccountid_value\",\"Value\":\"9f31bb4c-9125-ee11-9cbc-000d3ab1669a\"},{\"Key\":\"_parentaccountid_value@Microsoft.Dynamics.CRM.associatednavigationproperty\",\"Value\":\"parentaccountid\"},{\"Key\":\"_parentaccountid_value@Microsoft.Dynamics.CRM.lookuplogicalname\",\"Value\":\"account\"}],\"EntityState\":null,\"FormattedValues\":[],\"RelatedEntities\":[],\"RowVersion\":null,\"KeyAttributes\":[]}],\"MoreRecords\":false,\"PagingCookie\":null,\"MinActiveRowVersion\":null,\"TotalRecordCount\":0,\"TotalRecordCountLimitExceeded\":false,\"EntityName\":null}}],\"EntityState\":null,\"FormattedValues\":[],\"RelatedEntities\":[],\"RowVersion\":null,\"KeyAttributes\":[]}],\"MoreRecords\":false,\"PagingCookie\":null,\"MinActiveRowVersion\":null,\"TotalRecordCount\":0,\"TotalRecordCountLimitExceeded\":false,\"EntityName\":null}"
}
]
So, in testing this I can see the collection has the data but it doesn't show. I even added those extra fields to the related record.
Is it possible to do this?
Uhm probably this is because that fields is not just an entity collection, but something more complex that contains also the entity collection (but this is just my idea).
One workaround for this could be use the open type. Instead return an EntityCollection of Account, your custom api could return an entity collection of expando entities
Use open types with custom APIs - Power Apps | Microsoft Learn
But actually there is an error about sending array, so I don't know if this could be a problem also with return an entity collection inside it
Ok. Gotcha. I would recommend trying to populate the following collection if you want to populate related data - https://learn.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.entity.relatedentities?view=dataverse-sdk-latest#microsoft-xrm-sdk-entity-relatedentities
I haven't tried it but I assume that's the way to go.
Yes, of course.
Call example:
fetch(Xrm.Utility.getGlobalContext().getClientUrl() + "/api/data/v9.2/accounts?$select=name&$expand=account_parent_account($select=accountid,name)&$filter=accountid eq 9f31bb4c-9125-ee11-9cbc-000d3ab1669a", {
method: "GET",
and as a result:
[
{
"@odata.etag": "W/\"8756564\"",
"name": "Fourth Coffee (exemplo)",
"accountid": "9f31bb4c-9125-ee11-9cbc-000d3ab1669a",
"account_parent_account": [
{
"@odata.etag": "W/\"8650939\"",
"accountid": "a131bb4c-9125-ee11-9cbc-000d3ab1669a",
"name": "Litware, Inc. (exemplo)",
"_parentaccountid_value@OData.Community.Display.V1.FormattedValue": "Fourth Coffee (exemplo)",
"_parentaccountid_value@Microsoft.Dynamics.CRM.associatednavigationproperty": "parentaccountid",
"_parentaccountid_value@Microsoft.Dynamics.CRM.lookuplogicalname": "account",
"_parentaccountid_value": "9f31bb4c-9125-ee11-9cbc-000d3ab1669a"
},
{
"@odata.etag": "W/\"8756307\"",
"accountid": "a331bb4c-9125-ee11-9cbc-000d3ab1669a",
"name": "Adventure Works (exemplo)",
"_parentaccountid_value@OData.Community.Display.V1.FormattedValue": "Fourth Coffee (exemplo)",
"_parentaccountid_value@Microsoft.Dynamics.CRM.associatednavigationproperty": "parentaccountid",
"_parentaccountid_value@Microsoft.Dynamics.CRM.lookuplogicalname": "account",
"_parentaccountid_value": "9f31bb4c-9125-ee11-9cbc-000d3ab1669a"
}
],
"account_parent_account@odata.nextLink": "https://example.crm4.dynamics.com/api/data/v9.2/accounts(9f31bb4c-9125-ee11-9cbc-000d3ab1669a)/account_parent_account?$select=accountid,name"
}
]
It can display both the account and related accounts.
@T1ago wrote:
since the dataverses API shows related records and it makes sense that I should be able to do this as well
Can you please explain what you mean by that?
Hi there,
Yes, that's what I'm going to do. I was wondering if I was missing something since the dataverses API shows related records and it makes sense that I should be able to do this as well.
Maybe someone knows how to, but I do have a way to solve it like you suggested.
Hello,
Why not use string as output, inside your code serialize your data and deserialize on the clientside? In this case you can do whatever you want with that data on the client side. I used this approach on multiple projects and it worked like a charm.