web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to add a related entity through organization service API

(0) ShareShare
ReportReport
Posted on by

Hello,

I have an entity A and a related entity B. The relationship is 1:N b/w A and B.

Plugin is run when update message is triggered for object of entity A. Now, i need to add some objects of entity type B and attach them to the object of entity A.

I am using organization service context - "unit of work pattern".
I have done something like following and it is successful:
Guid entityBid = setup.serviceUser.Create(objectB);
relations.Add(new EntityReference(A.EntityLogicalName, entityBid));

var relationship = new Relationship("relation_name");
setup.serviceUser.Associate("A_name", objectA.Id, relationship, relations);
But, this does not conform to the "unit of work pattern" principle, as i am using context to manage the transaction.
If any exception happens afterwards before serviceContext.saveChanges(), all dirty objects should NOT be saved to the server, but by using above method, i have actually saved the relationship to the server even if some exception occurs later.
Is there any general proposed way to do this by using context only?

*This post is locked for comments

I have the same question (0)
  • ashlega Profile Picture
    34,477 on at

    Hi,

     Did you try using addLink?

    msdn.microsoft.com/.../microsoft.xrm.sdk.client.organizationservicecontext.addlink.aspx

     although, I am not sure if SaveChanges is starting a separate transaction (don't think so). But, if you are doing this in the pre/post operation plugin, you might try throwing an error from the plugin - that would cancel the whole SQL transaction, so all those changes would be rolled back (including the association, I believe)

  • Community Member Profile Picture
    on at

    Hi Aseem,

    As per your code, the relations means EntityReferenceCollection right?. But the EntityReference part you are assigning A.EntityLogicalName with entityBid instead of B.EntityLogicalName. So you need to change the code as,

    Guid entityBid = setup.serviceUser.Create(objectB);
    relations.Add(new EntityReference(B.EntityLogicalName, entityBid));

    var relationship = new Relationship("relation_name");
    setup.serviceUser.Associate("A_name", objectA.Id, relationship, relations);

  • Suggested answer
    Michel Gueli Profile Picture
    982 on at

    Maybe this will help:

    var account = new Entity("account");

    account.RelatedEntities.Add(new Relationship("contact_customer_accounts"), contacts);

    contacts is an EntityCollection of contact entities.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans