Search code examples
c#.netsoapsalesforce

How to update task status


Can anyone please shed some light on why the following code doesn't change task status, even though the call returns with a success message:

public bool markTaskCompleted(String id)
{
        try
        {
            sObject UpdateTask = new sObject();
            UpdateTask.type = "Task";

            UpdateTask.Id = id;

            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            XmlElement afield = doc.CreateElement("Status");
            XmlText txtNode = doc.CreateTextNode("Completed");
            afield.AppendChild(txtNode);

            SaveResult[] saveResults = binding.update(new sObject[] { UpdateTask });

            if (saveResults[0].success)
                return true;
            else
            {
                for (int i = 0; i < saveResults[0].errors.Length; i++)
                {
                    Error err = saveResults[0].errors[i];
                    System.Windows.Forms.MessageBox.Show("Errors were found on item 0" + Environment.NewLine +
                                                            "Error code: " + err.statusCode.ToString() + Environment.NewLine +
                                                            "Error message: " + err.message);
                }

                return false;
            }
        }
        catch (System.Web.Services.Protocols.SoapException e)
        {
            System.Windows.Forms.MessageBox.Show("An unexpected error has occurred: " + e.Message + " Stack trace: " + e.StackTrace);
            return false;
        }
}

Solution

  • Found it myself. I was missing the following critical line:

    UpdateTask.Any = new System.Xml.XmlElement[] { afield };
    

    This line should be added immediately after the afield.AppendChild(txtNode); line. Now it is updating the status just fine.