Copy all activities associated with entity to other entity thru Plugin in MS Dyanmcis
Requirement: Suppose you are copying over a record from an entity to another entity and you wanted to copy over the realted activities as well. Suppose you a custom Prospect entity record which have 2 task activites associated with, if you have to copy both tasks to another entity lets says Lead entity along with data.
if (entity.LogicalName == "new_prospect")
{
try
{
Entity lead = new Entity("lead");
ColumnSet cols = new ColumnSet();
cols.AddColumns(new string[] { "new_pipelinestatus", "new_rating", "new_lastname", "new_firstname", "new_businessphone", "new_addressstreet1", "new_city", "new_stateprovince", "new_zippostalcode", "new_updatecounter" });
entity = service.Retrieve("new_prospect", entity.Id, cols);
Guid Lid= service.Create(lead);
// copy activities
var query = new QueryExpression("task");
query.ColumnSet = new ColumnSet(true);
//regardingobjectid this will get all activities of Prospect entity.
query.Criteria.AddCondition(new ConditionExpression("regardingobjectid", ConditionOperator.Equal, entity.Id));
var activities = service.RetrieveMultiple(query).Entities;
foreach (var activity in activities)
{
activity.Attributes.Remove("activityid");
activity.Id = Guid.NewGuid();
string regardingaccountype = "lead";
activity["regardingobjectid"] = new EntityReference(regardingaccountype, Lid);
service.Create(activity);
}
No comments:
Post a Comment