locked
Copy Activities from One entity to Another entity approach RRS feed

  • Question

  • Hello Gurus,

    My last approach to copy record from one entity to another entity using plug-ins was a success. I have a requirement not only to copy the field values from one entity to another but also the activities associated with them, like task, notes and activities.

    How to use the existing plug-in to copy over the activities also.

    Is there any way to move/copy the related records of the related entities to another entity.

    Please help.

    Thank you all looking into my post.


    Puneet Joshi - Dynamics CRM Developer

    Saturday, October 20, 2012 5:37 PM

Answers

  • Hello,

    I think you can do this by retrieving all the related activities first, then assigning them a new Id (in order to clone the record) and the new "regardingobjectid" (to relate them to your new copy). Something like this:

    var query = new QueryExpression("activitypointer");
    query.Conditions.Add("reagrdingobjectid", ConditionOperator.Equal, theIdOfTheRelatedRecord);
    query.ColumnSet = new ColumnSet(true);
    
    var activities = service.RetrieveMultiple(query).Entities;
    
    foreach (var activity in activities)
    {
       activity.Id = Guid.NewGuid();
       activity["regardingobjectid"] = new EntityReference("copiedEntityName", copiedEntityId);
       service.Create(activity);
    }

    You can also take a look at this thread and at this thread for some info on cloning records or take a look at this post.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.


    Sunday, October 21, 2012 8:48 AM

All replies

  • Hello,

    I think you can do this by retrieving all the related activities first, then assigning them a new Id (in order to clone the record) and the new "regardingobjectid" (to relate them to your new copy). Something like this:

    var query = new QueryExpression("activitypointer");
    query.Conditions.Add("reagrdingobjectid", ConditionOperator.Equal, theIdOfTheRelatedRecord);
    query.ColumnSet = new ColumnSet(true);
    
    var activities = service.RetrieveMultiple(query).Entities;
    
    foreach (var activity in activities)
    {
       activity.Id = Guid.NewGuid();
       activity["regardingobjectid"] = new EntityReference("copiedEntityName", copiedEntityId);
       service.Create(activity);
    }

    You can also take a look at this thread and at this thread for some info on cloning records or take a look at this post.

    Greetings,

    Pavlos


    Please mark this reply as an answer and vote it as helpful if it helps you find a resolution to your problem.
    View my latest gallery contribution here.
    Visit my blog here.


    Sunday, October 21, 2012 8:48 AM
  • Any other explanation/code/links/blogs is also more than welcome, i understood the concept behind, but if anyone can throw some more light, that would be great.

    Thanks everybody.


    Puneet Joshi - Dynamics CRM Developer

    Thursday, October 25, 2012 7:46 PM
  • query.Conditions

    Here I am getting error in 2011. can i use criteria insted


    Saturday, July 13, 2013 9:17 PM
  • QueryExpression qe = new QueryExpression(); qe.EntityName = "account"; qe.ColumnSet = new ColumnSet(); qe.ColumnSet.Columns.Add("name"); qe.LinkEntities.Add(new LinkEntity("account", "contact", "primarycontactid", "contactid", JoinOperator.Inner)); qe.LinkEntities[0].Columns.AddColumns("firstname", "lastname"); qe.LinkEntities[0].EntityAlias = "primarycontact"; EntityCollection ec = _orgService.RetrieveMultiple(qe); Console.WriteLine("Retrieved {0} entities", ec.Entities.Count); foreach (Entity act in ec.Entities) { Console.WriteLine("account name:" + act["name"]); Console.WriteLine("primary contact first name:" + act["primarycontact.firstname"]); Console.WriteLine("primary contact last name:" + act["primarycontact.lastname"]); }

    For more info check : http://msdn.microsoft.com/en-us/library/gg328149.aspx


    Puneet Joshi - Dynamics CRM Developer

    Saturday, July 13, 2013 9:42 PM