Skip to main content
We are trying to move a task from one contact to another in a queueable class. The task is created by ListEmail functionality. We are trying to do this via APEX because an external system (of record) is dictating that some child objects of one contact need to be moved to another contact. Per the sample code below, we've tried executing this via updating or inserting a Task, and updating or inserting a TaskRelation. But all give the same error. Any insight into this area (i.e. ListEmail) of the Salesforce data model, how we can overcome these errors, etc would be helpful.

Note: Activity logging for ListEmail is enabled.

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);

 
1 answer
  1. May 9, 2018, 1:26 AM
    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;])
0/9000