Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to add a related entity through organization service API

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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

  • Suggested answer
    Michel Gueli Profile Picture
    Michel Gueli 982 on at
    RE: How to add a related entity through organization service API

    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.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to add a related entity through organization service API

    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);

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: How to add a related entity through organization service API

    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)

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans