Chirag
Chirag

Reputation: 334

Clone Phonecall entity

I am going to clone a phone call record, for that i do following

Entity originalToDo = crmService.Retrieve("phonecall", new Guid(originalToDoId), cols);

Entity cloneToDo = originalToDo;

and remove activityid and activitypartyid

if (originalToDo.Attributes.Contains("to"))
                {
                    foreach (Entity item in ((EntityCollection)(originalToDo.Attributes["to"])).Entities)
                    {
                        RemoveProperties(item, "activityid");
                        RemoveProperties(item, "activitypartyid");

                    }

                }

cloneToDo.Attributes["to"] = (EntityCollection)originalToDo.Attributes["to"];

 CreateRequest crRequest = new CreateRequest();  crRequest.Target = cloneToDo;

CreateResponse crResponse = (CreateResponse)service.Execute(crRequest);

But above line gives me an exception : Cannot insert duplicate key

Please suggest me, what i have missing ?

Upvotes: 1

Views: 393

Answers (4)

Shahna Panchami
Shahna Panchami

Reputation: 1

if (activity.Attributes.Contains("to"))
{
    foreach (Entity item in ((EntityCollection)(activity.Attributes["to"])).Entities)
    {
         item.Attributes.Remove("activityid");
         item.Attributes.Remove("activitypartyid");
         item.Id = Guid.NewGuid();
    }

    ap.Attributes["to"] = (EntityCollection)activity.Attributes["to"];
}

if (activity.Attributes.Contains("from"))
{
     foreach (Entity item in ((EntityCollection)(activity.Attributes["from"])).Entities)
     {
          item.Attributes.Remove("activityid");
          item.Attributes.Remove("activitypartyid");
          item.Id=Guid.NewGuid();
     }

     ap.Attributes["from"] = (EntityCollection)activity.Attributes["from"];
}

Upvotes: 0

iair007
iair007

Reputation: 92

I was having the same problem, and after trying all the solutions you post here couldn't make it work. I kept trying and found a solution to my problem. I was copying the To field as I got it from the source PhoneCall, and don't know why the system didn't like it. So the solution for me was, to re-create the EntityColletion to se the TO as I show here:

List<Entity> to = phoneCall.To != null ? phoneCall.To.ToList()
                                   : postImage != null & postImage.To != null ? postImage.To.ToList()
                                   : null;    
EntityCollection toCollection = new EntityCollection();
foreach (Entity t in to) {
     Entity toParty = new Entity("activityparty");
     toParty["partyid"] = t.GetAttributeValue<EntityReference>("partyid");   
     toCollection.Entities.Add(toParty);
}

And then Just set the "TO" attribute of the new PhoneCall with this new created EntityCollection ("toCollection") Hope this will help some one :)

Upvotes: 0

Chirag
Chirag

Reputation: 334

I have got the success from the following code :

First i have remove activityid from my clone entity

cloneToDo.Attributes.Remove("activityid");

Then i have added a new GUID to entity

cloneToDo.Id = Guid.NewGuid();

and its done.

NOTE : its better to remove statecode and statuscode

cloneToDo.Attributes.Remove("statecode");
cloneToDo.Attributes.Remove("statuscode");

Upvotes: 1

BlueSam
BlueSam

Reputation: 1888

It looks like the Id has not been cleared out. I've had success using the code below to properly clear it.

originalToDo.EntityState = null;    
originalToDo.Id = Guid.Empty;
originalToDo.Attributes.Remove("activityid");

Upvotes: 1

Related Questions