Posting here sample codes:
// 1. Task Update
List<Task> task = [SELECT Id, RecordTypeId, WhoId, WhatId, WhoCount, WhatCount, Subject, Status, Priority, AccountId, CreatedDate, LastModifiedDate, TaskSubtype, AccountId__c, Active__c FROM Task where Whoid ='0031N00001UvZDlQAN'];
Task tt = task.get(0); //assuming contact has only one task created by ListEmail
tt.WhoId = '0031N00001V70gwQAB'; //assigning the new (target) contact
database.update(tt);
// 2. Task clone and Insert
List<Task> task = [SELECT Id, RecordTypeId, WhoId, WhatId, WhoCount, WhatCount, Subject, Status, Priority, AccountId, CreatedDate, LastModifiedDate, TaskSubtype, AccountId__c, Active__c FROM Task where Whoid ='0031N00001UvZDlQAN'];
Task ttt = task.get(0); //assuming contact has only one task created by ListEmail
Task newTask = ttt.clone(false,true,true,true);
newTask.WhoId = '0031N00001V70gwQAB'; //assigning the new (target) contact
database.delete(ttt);
database.upsert(newTask);
// 3. Cloning task relation and updating relationId
TaskRelation tr = [SELECT Id, RelationId, TaskId, IsWhat, AccountId FROM TaskRelation where RelationId = '0031N00001UvZDlQAN'];
TaskRelation newTaskRelation = tr.clone(false,true,true,true);
newTaskRelation.RelationId = '0031N00001V70gwQAB'; //assigning the new (target) contact relation
database.delete(tr);
Database.upsert(newTaskRelation);
This is the error I am getting: (Database.Error[getFields=(WhatId);getMessage=Related To ID: id value of incorrect type: 0XB1N000000XaCuWAK;getStatusCode=FIELD_INTEGRITY_EXCEPTION;])