function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
randombardrandombard 

Update task whoid on contact delete

For some reason this works fine when i replace the :C.ID with a hard coded ID.

 

I have used this method before but not with .old so I am wondering if that changes something.

 

trigger MigrateRelatedObjects on Contact (before delete)
{
for(Contact C: trigger.old)
if([select id from Lead where Old_Contact_ID__c = :C.Id].size() > 0 )
{
For(Lead L:[select id from Lead where Old_Contact_ID__c = :C.Id])
    {List<Task> TaskToMove = new List<Task>();
    {For(Task T:[select id, WhoID from Task where WhoID = :C.Id])
    {
    T.WhoID = L.Id;
    TaskToMove.add(T);
    }
Update TaskToMove;
     System.debug('Success');
}
}
}
else
{
System.debug('No Lead Found');
}
}

Best Answer chosen by Admin (Salesforce Developers) 
randombardrandombard

I ended up modifiying the code and running it from the lead.

 

Got the result I wa looking for.

 

trigger MigrateRelatedOb on lead (before insert, before update) 
{
for(lead L: trigger.new)
if((L.Old_Contact_ID__c<>''))
{
//move tasks
For(Contact C:[select id from Contact where Contact.ID = :L.Old_Contact_ID__c])
    {
    List<Task> TaskToMove = new List<Task>();
    {For(Task T:[select id, WhoID from Task where WhoID = :C.Id])
    {
    T.WhoID = L.Id;
    TaskToMove.add(T);
    }
Update TaskToMove;
     System.debug('Success');
}
{
//move events
List<Event> EventToMove = new List <Event>();
{For(Event E:[select id, WhoID from Event where WhoID = :C.Id])
    {
    E.WhoID = L.Id;
    EventToMove.add(E);
    }
Update EventToMove;
     System.debug('Success');
}
}
//delete The contact
delete C;
}
}
else
{
System.debug('No Lead Found');
}
}