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
Shirley MaglioShirley Maglio 

Not able to update the WhoId field on Task and Event records

Hi all,

I have a batch process that updates the WhoId field of Task records.  When this batch process executes, I am currently getting the following error:
Update failed. First exception on row 0 with id 00T0n000003j9xABCW; first error: INVALID_CROSS_REFERENCE_KEY, Updating the whoId fields for shared activities is not allowed.: [WhoId]

Share Activites are already enabled in my Salesforce org.  The below is the code snippet:
global class BatchMerge implements Database.Batchable<clsMerge>, Database.Stateful, Database.AllowsCallouts{

	global class clsMerge {
            Id WinId, LosingId;
            String WinSFDCPartyId, LosingSFDCPartyId;
            public clsMerge(String strWinID, String strLosingId){
                this.WinID = strWinID;
                this.LosingId = strLosingId;
    global void execute(Database.BatchableContext context, List<object> scope){

        for(object objectScope : scope) {
            clsMerge objectMerge = (clsMerge)objectScope;                
			String WinSFDCPartyId = objectMerge.WinId;
            String LosingSFDCPartyId = objectMerge.LosingId;
            list<Task> lstTask = new list<Task>();

            for(Task objTask : [Select Id, WhoId From Task where WhoID=:LosingSFDCPartyId]){
                objTask.WhoID = WinSFDCPartyId;

            if(lstTask.size() > 0 && lstTask <> null)
                update lstTask;

How can I resolve the above error?  Any help is greatly appreciated!



Also at this moment, updating WhoId field for Task records when Shared Activities for the org is enabled, is not permitted from with Apex Class or Visualforce. However, this can be accomplished using the Standard Salesforce user interface. There is an Idea thread ( available requesting for this feature which you can vote for.

In case, your business logic requires this Id to be stored, you can create a custom field of type Text which stores the Record Id value or 2 different custom Lookup fields which can be updated with a Lead or a Contact Id based on the business logic. The advantage with lookups is that they will enable direct linking and viewing of related records on standard Salesforce screens. 

Also, one of the things I noticed in your code is Record Id values are being stored as String values. Record Id values, although mutable and can be stored as String variables should be stored as a variable of Id datatype as a part of the best practice. This results in accurate Id comparison between 15 and 18 digit Record Id versions of the same Record Id value.

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.
Shirley MaglioShirley Maglio
Hi Jigarshah,

Thank you for your response.

One interesting observation to note is:  the update on the WhoID field does work, when it is being done via a future method within a non-batch Apex class, but this cannot be done within a batch Apex class (which is what I am trying do), because future methods cannot be called from a batch process in Salesforce.


Please mark this thread as SOLVED if this answer has helped address your issue.