I found the solution. The error was "cannot update activity when email is in draft state".
Initially the plugin was registered under Post-Operation mode, so for some reason which I don't know, the record was not getting updated as I was explicitly trying to update the record using Service.update(Entity) method.
But after registering the plugin under Pre-Operation mode and removing the code:
Service.update(Entity)
the record was getting updated in email activity.
Also I found the way to update Party List fields. The corrected method is provided below:
private void UpdateEntity(ContactLookupResult result)
{
// Sender setup
EntityCollection senderCollection = new EntityCollection();
Entity senderActParty = new Entity(ActivityParty.EntityName);
if (result.SenderContactId.Value == Guid.Empty)
{
senderActParty[ActivityParty.AddressUsed] = result.SenderContactId.Key;
}
else
{
senderActParty[ActivityParty.PartyId] = new EntityReference(Contact.EntityName, result.SenderContactId.Value);
}
senderCollection.Entities.Add(senderActParty);
currentEntity[Email.Sender] = senderCollection;
// Recipients setup
EntityCollection recipientCollection = new EntityCollection();
foreach (var item in result.RecipientsId)
{
Entity entity = new Entity(ActivityParty.EntityName);
if (item.Value == Guid.Empty)
{
entity[ActivityParty.AddressUsed] = item.Key;
}
else
{
entity[ActivityParty.PartyId] = new EntityReference(Contact.EntityName, item.Value);
}
recipientCollection.Entities.Add(entity);
}
currentEntity[Email.To] = recipientCollection;
// Cc setup
EntityCollection ccCollection = new EntityCollection();
foreach (var item in result.CcId)
{
Entity entity = new Entity(ActivityParty.EntityName);
if (item.Value == Guid.Empty)
{
entity[ActivityParty.AddressUsed] = item.Key;
}
else
{
entity[ActivityParty.PartyId] = new EntityReference(Contact.EntityName, item.Value);
}
ccCollection.Entities.Add(entity);
}
currentEntity[Email.Cc] = ccCollection;
}
Hope this will be helpful for others.
Thank you !!!