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