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
RobmdRobmd 

Updating Task WhoId

Hello, this is a cross-post from the General Development area.  It saw no uptake, and I could really use some help so I am trying the more specific board.
 
I am having problems updating the WhoId property of a Task.  Here's the code block (contains some custom objects and variables declared outside this block but hopefully they are named well enough to be obvious)
 
A brief disclaimer: the code currently contains bits and pieces of the mud I threw against the wall trying to get some results to stick.  I would be happy to try different things.
 
Code:
string qTasks = "SELECT WhoId, Status, Id, Subject FROM Task WHERE WhatId = '" + opportunityId + "' AND Status = 'Not Started'";
apex.QueryResult qrTasks = sf.query( qTasks );

if( qrTasks.size > 0 )
{
 sObject[] Tasks = qrTasks.records;

 lineDetail = new System.Xml.XmlElement[2];
 lineDetail[0] = doc.CreateElement( "WhoId" );   

 foreach( sObject task in Tasks )
 {
  lineDetail[0] = task.Any[0];
  lineDetail[0].InnerXml = leadList[leadCount].ContactId;

  lineDetail[1] = task.Any[1];

  task.Any = lineDetail;
 }
 
 apex.SaveResult[] srTasks = sf.update( Tasks );

 for( int itemCount = 0; itemCount < srTasks.Length; itemCount++ )
 {
  if( srTasks[itemCount].success )
  {
   log( "leadProcessed(): saving Tasks successful!", 0 );
  } 
  else
  {
   log( "leadProcessed(): saving Tasks FAILED!!", 0 );
  }
 }
}

 I am trying to automate what is currently a manual process in our environment, converting leads and setting up the opportunity.  The step I am having problems with is after the lead is converted and most of the opportunity properties are updated.  The last step for our salespeople here is to associate all the Open Activities with the Contact that was just created from the lead.
 
In the SF Interface, this is done by clicking the "edit" link beside each Open Activity and setting the field labelled "Name" to "Contact" and the name of the freshly created Contact.
 
When I query a Task updated this way manually, the only change appears to be to the WhoId field, which references the Contact's Id field. 
 
When I run the block of code pasted above, Salesforce returns success for all tasks updated and the LastModifiedDate is updated to the time of execution, but the Task's WhoId remains null.
 
leadList[leadCount].ContactId containts the correct Id, it is thee one returned in the LeadConvertResult and when I go to x.salesforce.com/[ContactId Value] it loads with the correct contact.
 
I am using the Partner WSDL.
 
The WhatId field of the Task is referencing the Opportunity.
 
Many thanks for any suggestions about how I've gone wrong.
RobmdRobmd

Workaround:

I'm not a fan of this solution, but by creating a new array of Task sObjects to update, the WhoId is saved.  If possible, having a dev clarify why the update that uses the array of sObjects returned by the query doesn't work (or what I've done wrong) would be helpful.

Here's an example of code I got to work (probably in need of a bit of refactoring).

Code:

string qTasks = "SELECT WhoId, WhatId, Id FROM Task WHERE WhatId = '" + opportunityId + "' AND Status = 'Not Started'";
apex.QueryResult qrTasks = sf.query( qTasks );

XmlElement[] taskFields;
apex.sObject task;
apex.sObject[] tasks;
if( qrTasks.size > 0 )
{
 tasks = new sObject[qrTasks.size];
 for( int taskCount = 0; taskCount < qrTasks.size; taskCount++ )
 {
  taskFields = new XmlElement[1];
  task = new sObject();
  task.type = "Task";
  task.Id = qrTasks.records[taskCount].Id;

  taskFields[0] = doc.CreateElement( "WhoId" );
  taskFields[0].InnerText = leadList[leadCount ].ContactId;

  task.Any = taskFields;

  tasks[taskCount] = task;
 }

 apex.SaveResult[] srTasks = sf.update( tasks );
}