Skip to content

Instantly share code, notes, and snippets.

@pomu0325
Created November 27, 2015 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pomu0325/8b55608a9ca02dd5390b to your computer and use it in GitHub Desktop.
Save pomu0325/8b55608a9ca02dd5390b to your computer and use it in GitHub Desktop.
update Task.WhoId to ConvertedContactId for invisible Tasks related to converted Leads (Shared Activities enabled)
Map<Id, Lead> ls = new Map<id, lead>([select Id, ConvertedAccountId, ConvertedContactId from Lead where IsConverted = true]);
Task[] ts = [select WhoId, WhatId, (select Id from TaskRelations) from Task where WhoId in :ls.keySet()];
system.debug(ts.size());
TaskRelation[] deletes = new TaskRelation[]{};
TaskRelation[] inserts = new TaskRelation[]{};
for (Task t : ts) {
for (TaskRelation tr : t.TaskRelations) {
deletes.add(tr);
}
TaskRelation tr = new TaskRelation();
tr.TaskId = t.Id;
tr.RelationId = ls.get(t.WhoId).ConvertedContactId;
inserts.add(tr);
}
delete deletes;
insert inserts;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment