Retrieve N:N relationship Dynamics CRM
I have a relationship between Opportunities and my custom Contract entity in Dynamics 2016 on premise. I am trying to retrieve all of the related contracts from a particular opportunity in a C# plugin. When I try to retrieve the relationships, I receive the error:
No system many-to-many relationship exists between opportunity and ccseq_contract. If attempting to link through a custom many-to-many relationship ensure that you provide the from and to attributes.
It appears that the relationship does exist based on this screenshot:
Here's my Query Expression:
EntityCollection contracts = service.RetrieveMultiple(new QueryExpression()
{
EntityName = Opportunity.LogicalName,
ColumnSet = new ColumnSet(new String
{
Opportunity.Properties.OpportunityId
}),
LinkEntities =
{
new LinkEntity
{
LinkFromEntityName = Opportunity.LogicalName,
LinkToEntityName = Contract.LogicalName,
LinkCriteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = Opportunity.Properties.OpportunityId,
Operator = ConditionOperator.Equal,
Values = {wonOpportunity.Id}
}
}
}
}
}
});
Why am I receiving this error and how can I resolve the error?
c# dynamics-crm microsoft-dynamics
add a comment |
I have a relationship between Opportunities and my custom Contract entity in Dynamics 2016 on premise. I am trying to retrieve all of the related contracts from a particular opportunity in a C# plugin. When I try to retrieve the relationships, I receive the error:
No system many-to-many relationship exists between opportunity and ccseq_contract. If attempting to link through a custom many-to-many relationship ensure that you provide the from and to attributes.
It appears that the relationship does exist based on this screenshot:
Here's my Query Expression:
EntityCollection contracts = service.RetrieveMultiple(new QueryExpression()
{
EntityName = Opportunity.LogicalName,
ColumnSet = new ColumnSet(new String
{
Opportunity.Properties.OpportunityId
}),
LinkEntities =
{
new LinkEntity
{
LinkFromEntityName = Opportunity.LogicalName,
LinkToEntityName = Contract.LogicalName,
LinkCriteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = Opportunity.Properties.OpportunityId,
Operator = ConditionOperator.Equal,
Values = {wonOpportunity.Id}
}
}
}
}
}
});
Why am I receiving this error and how can I resolve the error?
c# dynamics-crm microsoft-dynamics
add a comment |
I have a relationship between Opportunities and my custom Contract entity in Dynamics 2016 on premise. I am trying to retrieve all of the related contracts from a particular opportunity in a C# plugin. When I try to retrieve the relationships, I receive the error:
No system many-to-many relationship exists between opportunity and ccseq_contract. If attempting to link through a custom many-to-many relationship ensure that you provide the from and to attributes.
It appears that the relationship does exist based on this screenshot:
Here's my Query Expression:
EntityCollection contracts = service.RetrieveMultiple(new QueryExpression()
{
EntityName = Opportunity.LogicalName,
ColumnSet = new ColumnSet(new String
{
Opportunity.Properties.OpportunityId
}),
LinkEntities =
{
new LinkEntity
{
LinkFromEntityName = Opportunity.LogicalName,
LinkToEntityName = Contract.LogicalName,
LinkCriteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = Opportunity.Properties.OpportunityId,
Operator = ConditionOperator.Equal,
Values = {wonOpportunity.Id}
}
}
}
}
}
});
Why am I receiving this error and how can I resolve the error?
c# dynamics-crm microsoft-dynamics
I have a relationship between Opportunities and my custom Contract entity in Dynamics 2016 on premise. I am trying to retrieve all of the related contracts from a particular opportunity in a C# plugin. When I try to retrieve the relationships, I receive the error:
No system many-to-many relationship exists between opportunity and ccseq_contract. If attempting to link through a custom many-to-many relationship ensure that you provide the from and to attributes.
It appears that the relationship does exist based on this screenshot:
Here's my Query Expression:
EntityCollection contracts = service.RetrieveMultiple(new QueryExpression()
{
EntityName = Opportunity.LogicalName,
ColumnSet = new ColumnSet(new String
{
Opportunity.Properties.OpportunityId
}),
LinkEntities =
{
new LinkEntity
{
LinkFromEntityName = Opportunity.LogicalName,
LinkToEntityName = Contract.LogicalName,
LinkCriteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = Opportunity.Properties.OpportunityId,
Operator = ConditionOperator.Equal,
Values = {wonOpportunity.Id}
}
}
}
}
}
});
Why am I receiving this error and how can I resolve the error?
c# dynamics-crm microsoft-dynamics
c# dynamics-crm microsoft-dynamics
asked Nov 15 '18 at 15:28
Tim HutchisonTim Hutchison
1,44812149
1,44812149
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
The LinkedEntity
in a query expression is exactly like a SQL inner or outer join (you specify the join type).
it's great for fetching a N:1 relationship, it doesn't really work for a N:N.
For the N:N, you need to go via the 'relationship entity'.
If you want all contracts linked to an opportunity,
you must retrieve all contacts that has a row linking them to that opportunity, in the 'relationship entity' table, 'ccseq_opportunity_ccseq_contract' (I'm using string constants below, because I don't quite know how you're building your entity classes).
var q = new QueryExpression("ccseq_contract") {
ColumnSet = new ColumnSet(true), //or specify what fields you want from ccseq_contract
LinkEntities = {
new LinkEntity() {
LinkFromEntityName = "ccseq_contract",
LinkToEntityName = "ccseq_opportunity_ccseq_contract",
ColumnSet = new ColumnSet(false), //don't fetch any fields from the link table
LinkCriteria = new FilterExpression() {
FilterOperator = LogicalOperator.And,
Conditions = {
new ConditionExpression("opportunityid", ConditionOperator.Equal, wonOpportunity.Id)
}
}
}
}
};
As an aside, when you're not using the 'in' query operator, I would really prefer using LINQ queries instead of query expressions, if you have generated strongly typed entity classes.
The LINQ query would look like
using(var ctx = new OrganizationServiceContext(service)) {
var contracts = (
from c in ctx.CreateQuery<ccseq_contract>()
join lnk in ctx.CreateQuery<ccseq_opportunity_ccseq_contract>() on c.ccseq_contractId equals link.ccseq_contractId
where lnk.opportunityid = wonOpportunity.Id
select c
// Or, to fetch only some fields, do
// select new { c.ccseq_contractId, c.ccseq_name }
).ToList();
}
add a comment |
Please try to retrieve list of contracts using the below XML query. The query is done on the N:N relationship.
<fetch mapping='logical'>
<entity name='ccseq_opportunity_ccseq_contract'>
<attribute name='opportunityid'/>
<attribute name='ccseq_contractid'/>
<link-entity name='opportunity' to='opportunityid' from='opportunityid' alias='opportunity'>
<attribute name='opportunityid'/>
<filter type='and'>
<condition attribute='opportunityid' operator='eq' value=$'{wonOpportunity.Id}'/>
</filter>
</link-entity>
</entity>
</fetch>
Hope it helps.
add a comment |
Here is where I ended up. This was based partially on gnud's answer.
QueryExpression query = new QueryExpression("ccseq_opportunity_ccseq_contract");
query.ColumnSet.AddColumns(Contract.Properties.ContractId, Opportunity.Properties.OpportunityId);
query.Criteria = new FilterExpression();
query.Criteria.AddCondition(Opportunity.Properties.OpportunityId, ConditionOperator.Equal, wonOpportunity.Id);
EntityCollection contracts = service.RetrieveMultiple(query);
This is fine if you just need the contract IDs. If you need something more for each contract, then it's a lot more efficient to join the contract table to the link table, than it is to do another query to load the contract attributes.
– gnud
Nov 19 '18 at 18:01
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322719%2fretrieve-nn-relationship-dynamics-crm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The LinkedEntity
in a query expression is exactly like a SQL inner or outer join (you specify the join type).
it's great for fetching a N:1 relationship, it doesn't really work for a N:N.
For the N:N, you need to go via the 'relationship entity'.
If you want all contracts linked to an opportunity,
you must retrieve all contacts that has a row linking them to that opportunity, in the 'relationship entity' table, 'ccseq_opportunity_ccseq_contract' (I'm using string constants below, because I don't quite know how you're building your entity classes).
var q = new QueryExpression("ccseq_contract") {
ColumnSet = new ColumnSet(true), //or specify what fields you want from ccseq_contract
LinkEntities = {
new LinkEntity() {
LinkFromEntityName = "ccseq_contract",
LinkToEntityName = "ccseq_opportunity_ccseq_contract",
ColumnSet = new ColumnSet(false), //don't fetch any fields from the link table
LinkCriteria = new FilterExpression() {
FilterOperator = LogicalOperator.And,
Conditions = {
new ConditionExpression("opportunityid", ConditionOperator.Equal, wonOpportunity.Id)
}
}
}
}
};
As an aside, when you're not using the 'in' query operator, I would really prefer using LINQ queries instead of query expressions, if you have generated strongly typed entity classes.
The LINQ query would look like
using(var ctx = new OrganizationServiceContext(service)) {
var contracts = (
from c in ctx.CreateQuery<ccseq_contract>()
join lnk in ctx.CreateQuery<ccseq_opportunity_ccseq_contract>() on c.ccseq_contractId equals link.ccseq_contractId
where lnk.opportunityid = wonOpportunity.Id
select c
// Or, to fetch only some fields, do
// select new { c.ccseq_contractId, c.ccseq_name }
).ToList();
}
add a comment |
The LinkedEntity
in a query expression is exactly like a SQL inner or outer join (you specify the join type).
it's great for fetching a N:1 relationship, it doesn't really work for a N:N.
For the N:N, you need to go via the 'relationship entity'.
If you want all contracts linked to an opportunity,
you must retrieve all contacts that has a row linking them to that opportunity, in the 'relationship entity' table, 'ccseq_opportunity_ccseq_contract' (I'm using string constants below, because I don't quite know how you're building your entity classes).
var q = new QueryExpression("ccseq_contract") {
ColumnSet = new ColumnSet(true), //or specify what fields you want from ccseq_contract
LinkEntities = {
new LinkEntity() {
LinkFromEntityName = "ccseq_contract",
LinkToEntityName = "ccseq_opportunity_ccseq_contract",
ColumnSet = new ColumnSet(false), //don't fetch any fields from the link table
LinkCriteria = new FilterExpression() {
FilterOperator = LogicalOperator.And,
Conditions = {
new ConditionExpression("opportunityid", ConditionOperator.Equal, wonOpportunity.Id)
}
}
}
}
};
As an aside, when you're not using the 'in' query operator, I would really prefer using LINQ queries instead of query expressions, if you have generated strongly typed entity classes.
The LINQ query would look like
using(var ctx = new OrganizationServiceContext(service)) {
var contracts = (
from c in ctx.CreateQuery<ccseq_contract>()
join lnk in ctx.CreateQuery<ccseq_opportunity_ccseq_contract>() on c.ccseq_contractId equals link.ccseq_contractId
where lnk.opportunityid = wonOpportunity.Id
select c
// Or, to fetch only some fields, do
// select new { c.ccseq_contractId, c.ccseq_name }
).ToList();
}
add a comment |
The LinkedEntity
in a query expression is exactly like a SQL inner or outer join (you specify the join type).
it's great for fetching a N:1 relationship, it doesn't really work for a N:N.
For the N:N, you need to go via the 'relationship entity'.
If you want all contracts linked to an opportunity,
you must retrieve all contacts that has a row linking them to that opportunity, in the 'relationship entity' table, 'ccseq_opportunity_ccseq_contract' (I'm using string constants below, because I don't quite know how you're building your entity classes).
var q = new QueryExpression("ccseq_contract") {
ColumnSet = new ColumnSet(true), //or specify what fields you want from ccseq_contract
LinkEntities = {
new LinkEntity() {
LinkFromEntityName = "ccseq_contract",
LinkToEntityName = "ccseq_opportunity_ccseq_contract",
ColumnSet = new ColumnSet(false), //don't fetch any fields from the link table
LinkCriteria = new FilterExpression() {
FilterOperator = LogicalOperator.And,
Conditions = {
new ConditionExpression("opportunityid", ConditionOperator.Equal, wonOpportunity.Id)
}
}
}
}
};
As an aside, when you're not using the 'in' query operator, I would really prefer using LINQ queries instead of query expressions, if you have generated strongly typed entity classes.
The LINQ query would look like
using(var ctx = new OrganizationServiceContext(service)) {
var contracts = (
from c in ctx.CreateQuery<ccseq_contract>()
join lnk in ctx.CreateQuery<ccseq_opportunity_ccseq_contract>() on c.ccseq_contractId equals link.ccseq_contractId
where lnk.opportunityid = wonOpportunity.Id
select c
// Or, to fetch only some fields, do
// select new { c.ccseq_contractId, c.ccseq_name }
).ToList();
}
The LinkedEntity
in a query expression is exactly like a SQL inner or outer join (you specify the join type).
it's great for fetching a N:1 relationship, it doesn't really work for a N:N.
For the N:N, you need to go via the 'relationship entity'.
If you want all contracts linked to an opportunity,
you must retrieve all contacts that has a row linking them to that opportunity, in the 'relationship entity' table, 'ccseq_opportunity_ccseq_contract' (I'm using string constants below, because I don't quite know how you're building your entity classes).
var q = new QueryExpression("ccseq_contract") {
ColumnSet = new ColumnSet(true), //or specify what fields you want from ccseq_contract
LinkEntities = {
new LinkEntity() {
LinkFromEntityName = "ccseq_contract",
LinkToEntityName = "ccseq_opportunity_ccseq_contract",
ColumnSet = new ColumnSet(false), //don't fetch any fields from the link table
LinkCriteria = new FilterExpression() {
FilterOperator = LogicalOperator.And,
Conditions = {
new ConditionExpression("opportunityid", ConditionOperator.Equal, wonOpportunity.Id)
}
}
}
}
};
As an aside, when you're not using the 'in' query operator, I would really prefer using LINQ queries instead of query expressions, if you have generated strongly typed entity classes.
The LINQ query would look like
using(var ctx = new OrganizationServiceContext(service)) {
var contracts = (
from c in ctx.CreateQuery<ccseq_contract>()
join lnk in ctx.CreateQuery<ccseq_opportunity_ccseq_contract>() on c.ccseq_contractId equals link.ccseq_contractId
where lnk.opportunityid = wonOpportunity.Id
select c
// Or, to fetch only some fields, do
// select new { c.ccseq_contractId, c.ccseq_name }
).ToList();
}
answered Nov 15 '18 at 15:57
gnudgnud
62.3k55070
62.3k55070
add a comment |
add a comment |
Please try to retrieve list of contracts using the below XML query. The query is done on the N:N relationship.
<fetch mapping='logical'>
<entity name='ccseq_opportunity_ccseq_contract'>
<attribute name='opportunityid'/>
<attribute name='ccseq_contractid'/>
<link-entity name='opportunity' to='opportunityid' from='opportunityid' alias='opportunity'>
<attribute name='opportunityid'/>
<filter type='and'>
<condition attribute='opportunityid' operator='eq' value=$'{wonOpportunity.Id}'/>
</filter>
</link-entity>
</entity>
</fetch>
Hope it helps.
add a comment |
Please try to retrieve list of contracts using the below XML query. The query is done on the N:N relationship.
<fetch mapping='logical'>
<entity name='ccseq_opportunity_ccseq_contract'>
<attribute name='opportunityid'/>
<attribute name='ccseq_contractid'/>
<link-entity name='opportunity' to='opportunityid' from='opportunityid' alias='opportunity'>
<attribute name='opportunityid'/>
<filter type='and'>
<condition attribute='opportunityid' operator='eq' value=$'{wonOpportunity.Id}'/>
</filter>
</link-entity>
</entity>
</fetch>
Hope it helps.
add a comment |
Please try to retrieve list of contracts using the below XML query. The query is done on the N:N relationship.
<fetch mapping='logical'>
<entity name='ccseq_opportunity_ccseq_contract'>
<attribute name='opportunityid'/>
<attribute name='ccseq_contractid'/>
<link-entity name='opportunity' to='opportunityid' from='opportunityid' alias='opportunity'>
<attribute name='opportunityid'/>
<filter type='and'>
<condition attribute='opportunityid' operator='eq' value=$'{wonOpportunity.Id}'/>
</filter>
</link-entity>
</entity>
</fetch>
Hope it helps.
Please try to retrieve list of contracts using the below XML query. The query is done on the N:N relationship.
<fetch mapping='logical'>
<entity name='ccseq_opportunity_ccseq_contract'>
<attribute name='opportunityid'/>
<attribute name='ccseq_contractid'/>
<link-entity name='opportunity' to='opportunityid' from='opportunityid' alias='opportunity'>
<attribute name='opportunityid'/>
<filter type='and'>
<condition attribute='opportunityid' operator='eq' value=$'{wonOpportunity.Id}'/>
</filter>
</link-entity>
</entity>
</fetch>
Hope it helps.
answered Nov 16 '18 at 7:12
Aakarsh DhawanAakarsh Dhawan
1196
1196
add a comment |
add a comment |
Here is where I ended up. This was based partially on gnud's answer.
QueryExpression query = new QueryExpression("ccseq_opportunity_ccseq_contract");
query.ColumnSet.AddColumns(Contract.Properties.ContractId, Opportunity.Properties.OpportunityId);
query.Criteria = new FilterExpression();
query.Criteria.AddCondition(Opportunity.Properties.OpportunityId, ConditionOperator.Equal, wonOpportunity.Id);
EntityCollection contracts = service.RetrieveMultiple(query);
This is fine if you just need the contract IDs. If you need something more for each contract, then it's a lot more efficient to join the contract table to the link table, than it is to do another query to load the contract attributes.
– gnud
Nov 19 '18 at 18:01
add a comment |
Here is where I ended up. This was based partially on gnud's answer.
QueryExpression query = new QueryExpression("ccseq_opportunity_ccseq_contract");
query.ColumnSet.AddColumns(Contract.Properties.ContractId, Opportunity.Properties.OpportunityId);
query.Criteria = new FilterExpression();
query.Criteria.AddCondition(Opportunity.Properties.OpportunityId, ConditionOperator.Equal, wonOpportunity.Id);
EntityCollection contracts = service.RetrieveMultiple(query);
This is fine if you just need the contract IDs. If you need something more for each contract, then it's a lot more efficient to join the contract table to the link table, than it is to do another query to load the contract attributes.
– gnud
Nov 19 '18 at 18:01
add a comment |
Here is where I ended up. This was based partially on gnud's answer.
QueryExpression query = new QueryExpression("ccseq_opportunity_ccseq_contract");
query.ColumnSet.AddColumns(Contract.Properties.ContractId, Opportunity.Properties.OpportunityId);
query.Criteria = new FilterExpression();
query.Criteria.AddCondition(Opportunity.Properties.OpportunityId, ConditionOperator.Equal, wonOpportunity.Id);
EntityCollection contracts = service.RetrieveMultiple(query);
Here is where I ended up. This was based partially on gnud's answer.
QueryExpression query = new QueryExpression("ccseq_opportunity_ccseq_contract");
query.ColumnSet.AddColumns(Contract.Properties.ContractId, Opportunity.Properties.OpportunityId);
query.Criteria = new FilterExpression();
query.Criteria.AddCondition(Opportunity.Properties.OpportunityId, ConditionOperator.Equal, wonOpportunity.Id);
EntityCollection contracts = service.RetrieveMultiple(query);
answered Nov 16 '18 at 14:52
Tim HutchisonTim Hutchison
1,44812149
1,44812149
This is fine if you just need the contract IDs. If you need something more for each contract, then it's a lot more efficient to join the contract table to the link table, than it is to do another query to load the contract attributes.
– gnud
Nov 19 '18 at 18:01
add a comment |
This is fine if you just need the contract IDs. If you need something more for each contract, then it's a lot more efficient to join the contract table to the link table, than it is to do another query to load the contract attributes.
– gnud
Nov 19 '18 at 18:01
This is fine if you just need the contract IDs. If you need something more for each contract, then it's a lot more efficient to join the contract table to the link table, than it is to do another query to load the contract attributes.
– gnud
Nov 19 '18 at 18:01
This is fine if you just need the contract IDs. If you need something more for each contract, then it's a lot more efficient to join the contract table to the link table, than it is to do another query to load the contract attributes.
– gnud
Nov 19 '18 at 18:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322719%2fretrieve-nn-relationship-dynamics-crm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown