web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Copying Activities from one Entity to another Entity

(0) ShareShare
ReportReport
Posted on by 3,315

Hi Experts,

Here I have requirement that copy all Activities from one entity to another entity. In Activities we are having Email,Task,Phone call etc. We can't copy all activities in single shot.So I am using switchcase statement for copying activities. But as all knows that switch case is not preferable for developers.So is there any other way to copy activities from one entity to other.? 

Its really helpful to me if anyone give idea to achieve this.

Here I mention the sample code.

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)
{
string activitytype = activity["activitytypecode"].ToString();

switch (activitytype)
{

case "email":

var myEmail = new Email();
myEmail["regardingobjectid"] = new EntityReference(logicalName, id);
myEmail["subject"] = activity.Attributes.Contains("subject") ? activity.Attributes["subject"] : null;

OrganizationService.Create(myEmail);
break;

case "task":
var myTask = new Entity("task");
myTask["regardingobjectid"] = new EntityReference(logicalName, id);
myTask.Attributes.Add("subject", activity.Attributes["subject"]);
OrganizationService.Create(myTask);
break;

}
}

Please suggest me better approach.

Thanks in advance.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    abhishek_255 Profile Picture
    1,614 on at

    Hi Shivaram,

    You can use Linq

    var activities = org1.RetrieveMultiple(query).Entities;

    var entitycollection = activities.Where(o => o.GetAttributeValue<string>("activitytypecode") == "email"

       | o.GetAttributeValue<string>("activitytypecode") == "task");

    foreach (var activityentity in entitycollection)

    {

       var activity = new Entity(activityentity.GetAttributeValue<string>("activitytypecode"));

       if (activityentity.Attributes.Contains("regardingobjectid"))

       activity["regardingobjectid"] = new EntityReference(logicalName, id);

       if (activityentity.Attributes.Contains("subject"))

           activity.Attributes.Add("subject", activityentity.GetAttributeValue<string>("subject"));

       OrganizationService.Create(activity);

    }

    You can also create Enum for activities and write logic to read

    var entitycollection = activities.Where("fetch only those values if matches to enum fields");

    In case my response helps you, please mark it as "Verified"

    Best Regards,

    Abhishek

  • shivaram Profile Picture
    3,315 on at

    Hi Abhishek,

    Thanks for your response. But here for sample I just mentioned only 2 activities. But fortunate or unfortunate we are having so many activities like Phonecall,appointment,letters,fax,email,task etc. For this I can't write switch case and I can't check all the time with getAttributeValue function for these Activities.

    Please suggest me. Thanks again for your response.

  • Suggested answer
    abhishek_255 Profile Picture
    1,614 on at

    Hi Shivaram,

    Given below is dummy code, I have not tested it.

    To copy and create a new entity, you can iterate through loop and copy attributes from one object to another. I have not tested it on deep or shallow copy.

    You need to extract "activitytypecode" at least once for each iteration.

    foreach (var activityentity in entitycollection){

    var activity = new Entity(activityentity.GetAttributeValue<string>("activitytypecode"));

    foreach (var attributecollectionitems in activityentity.Attributes ){

    if ((attributecollectionitems.Key != null | attributecollectionitems.Value != null) && attributecollectionitems.Key !="id")

    {

    //See if this works with shallow copy

    activity.Attributes.Add(attributecollectionitems.Key, attributecollectionitems.Value);
    }

    //below condition needs to be set for each new object..

    if (activityentity.Attributes.Contains("regardingobjectid"))

    activity["regardingobjectid"] = new EntityReference(logicalName, id);
    }

    OrganizationService.Create(activity);
    }

    If my response helps you, please mark it as "Verified"

    Best Regards,

    Abhishek

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Varsha deshpande Profile Picture

Varsha deshpande 5

#2
NeerajPawar Profile Picture

NeerajPawar 2

#2
jlattimer Profile Picture

jlattimer 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans