Debugging · Dynamics CRM 2011

CRM 2011: Entity role must be specified for reflexive relationship

I got an error when trying to add a M:N relationship record to the same entity (new_document_refer_document).

This is because CRM needs to figure out the direction of the relationship. To do this, simply populate the PrimaryEntityRole property of the Relationship parameter.

var request = new AssociateRequest
		Relationship = new Relationship
				SchemaName = "new_document_refer_document",
				PrimaryEntityRole = EntityRole.Referenced
		Target = new EntityReference("new_document", parentDocumentId),
		RelatedEntities = new EntityReferenceCollection 
				new EntityReference("new_document", childDocumentId)


Shout out to great post at mydynamicscrmblog.

PS: I noticed for self referencing M:N relationship, CRM appends ‘One’ and ‘Two’ on the lookup fields. eg. new_documentidOne, new_documentidTwo.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s