Hi Honan,
This was not an issue in earlier releases of CRM, but in 2016/365, the email can only be modified if the status is draft.
The easiest way is to set it as draft before making your changes, make the changes, and set it to Sent or Received again:
SetState("email", emailId, ActivityStateCode.Open, EmailStatusReason.Draft);
UpdateEmailFields(emailId);
SetState("email", emailId, ActivityStateCode.Completed, EmailStatusReason.Sent);
Here is your Set State function:
public void SetState(string entityname, Guid entityid, ActivityStateCode state, EmailStatusReason status)
{
EntityReference moniker = new EntityReference();
moniker.LogicalName = entityname;
moniker.Id = entityid;
SetStateRequest request = new SetStateRequest();
request.EntityMoniker = moniker;
request.State = new OptionSetValue((int)state);
request.Status = new OptionSetValue((int)status);
try
{
SetStateResponse response = (SetStateResponse)OrganizationService.Execute(request);
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException(ex);
}
}
You can use the following Activity State/Status codes:
enum ActivityStateCode : int
{ Open = 0, Completed = 1, Canceled = 2 }
enum EmailStatusReason : int
{ Draft = 1, Completed = 2, Sent = 3, Received = 4, Canceled = 5, PendingSend = 6, Sending = 7, Failed = 8 }
Hope this helps.