Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Create Entity (Case) record with Many-To-Many relationship to existing records

Posted on by

Hi,

I have Many-To-Many relationship between Case and a custom entity "Subject". There are already some records available for "Subject" which I want to associate with Case while creating a Case using SDK. I am using Entity.RelatedEntities to do it, however it works only where new "Subject" is created and associated with Case, not where I have to associate existing "Subject" with Case.

Following Code works and created both new Case and new Subject and associate it with each other in Many-To-Many relationship

Entity eCase = new Entity("incident");
                eCase["title"] = "Multi Tag Creation - Test Case created from Console " + DateTime.Now.ToString();
                eCase["customerid"] = new EntityReference("account", new Guid("C36159B1-325F-E811-A965-000D3A108AE0"));

                Entity tag1 = new Entity("poc_subject");
                tag1["ent_name"] = "Tag 0004";

                Entity tag2 = new Entity("poc_subject");
                tag2["ent_name"] = "Tag 0005";

                Entity tag3 = new Entity("poc_subject");
                tag3["ent_name"] = "Tag 0001";

                EntityCollection relatedTagsToCreate = new EntityCollection();
                relatedTagsToCreate.Entities.AddRange(tag1, tag2, tag3);

                Relationship tagRelationship = new Relationship("ent_incident_poc_subject");
                eCase.RelatedEntities.Add(tagRelationship, relatedTagsToCreate);
                service.Create(eCase);


However following code fails with error "Duplicate Key cannot be added"

Entity eCase = new Entity("incident");
                eCase["title"] = "Multi Tag Creation - Test Case created from Console " + DateTime.Now.ToString();
                eCase["customerid"] = new EntityReference("account", new Guid("C36159B1-325F-E811-A965-000D3A108AE0"));

                Entity tag1 = new Entity("poc_subject");
				//This is existing Subject record ID
                tag1["poc_subjectid"] = new Guid("DAC26AAC-9C84-E811-A967-000D3A10875D");

                Entity tag2 = new Entity("poc_subject");
				//This is existing Subject record ID
                tag2["poc_subjectid"] = new Guid("DAC26AAC-9C84-E811-A967-000D3A10876D");

                Entity tag3 = new Entity("poc_subject");
				//This is existing Subject record ID
                tag3["poc_subjectid"] = new Guid("DAC26AAC-9C84-E811-A967-000D3A10877D");

                EntityCollection relatedTagsToCreate = new EntityCollection();
                relatedTagsToCreate.Entities.AddRange(tag1, tag2, tag3);

                Relationship tagRelationship = new Relationship("ent_incident_poc_subject");
                eCase.RelatedEntities.Add(tagRelationship, relatedTagsToCreate);
                service.Create(eCase);


Has anyone tried associating existing related entities on create?

*This post is locked for comments

  • MaKeer Profile Picture
    MaKeer on at
    RE: Create Entity (Case) record with Many-To-Many relationship to existing records

    @Dyanamics, yes I think, we can combine only one action like, Create or Update. Same action is applied for RelatedEntities collection. However I have seen some sample code where it works in Web API, hence was expecting something working in SDK as well.

  • MaKeer Profile Picture
    MaKeer on at
    RE: Create Entity (Case) record with Many-To-Many relationship to existing records

    @ Sandeep, type of relationship is correct and it does work if I want to create new subject along with new Case, it fails only when I wanted to create new Case with existing Subject

  • MaKeer Profile Picture
    MaKeer on at
    RE: Create Entity (Case) record with Many-To-Many relationship to existing records

    @Drew, I can call Associate method, however I wanted to do it at the time of Case creation.

  • sandeepstw Profile Picture
    sandeepstw 4,601 on at
    RE: Create Entity (Case) record with Many-To-Many relationship to existing records

    Hi,

    Please check type of relation you are using in between both entities, I think you should use many to many relation.

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Create Entity (Case) record with Many-To-Many relationship to existing records

    Hi MaKeer,

    You are using compound create to create and associate 'Case' and 'Subject' entities. It works perfectly if both entities are new .If subjects are new , whatever you are doing is correct and you should continue using it over Associate   message as you  can create and associate  in one request and it takes care of roll back if there is a failure.(This is also being taken care natively  by plugins ).

    However, if  one of the entity record is an existing record, you can't use compound create and you will get "Duplicate Key cannot be added" error , which you are already getting.So if the subject is an existing record , use Assosiate message as recomended by  Drew Poggemann and Ravi Kashyap .

    Thanks.

    Please mark this answer as verified if it answers your question.

    use free tool  dynamicshero.com/scheduler to schedule Workflow and Actions.

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Create Entity (Case) record with Many-To-Many relationship to existing records

    Hi,

    I am not sure if this is the correct way to creating many to many relationship. As suggested above, you should use associate request-

    www.mscrmtutorial.com/.../associate-and-disassociate-many-to-many.html

    Hope this helps.

  • Verified answer
    Drew Poggemann Profile Picture
    Drew Poggemann 9,079 on at
    RE: Create Entity (Case) record with Many-To-Many relationship to existing records

    Hi MaKeer,

    Looking at the code you are creating subjects when I think you should be retrieving them and then associating them to the Case.

    See the following as good example code to help you through this using the Associate command:

    msdn.microsoft.com/.../gg309247.aspx

    Hope this helps.  

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans