web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Update Email Activity (Field type: Party List) using asynchronous plugin.

(0) ShareShare
ReportReport
Posted on by

I am trying to update the To, Sender & Cc fields of an email activity using a plugin. But I am getting an error says something like this "cannot update activity when email is in draft state". Also I need some assistance on how to update field of type 'party list'. 

I have attached a method which essentially updates the Email entity which is passed to the plugin via IServideProvider. 

private void UpdateActivity(ContactLookupResult result)
{

    Entity senderActParty = new Entity("activityparty");
    if (result.SenderContactId.Value == Guid.Empty)
    {
         senderActParty["addressused"] = result.SenderContactId.Key;
    }
   else
    {
     senderActParty["partyid"] = new EntityReference("contact", result.SenderContactId.Value);
    }
   currentEntity["from"] = senderActParty;


   EntityCollection recipientCollection = new EntityCollection();
   foreach (KeyValuePair<string,Guid> item in result.RecipientsId)
   {
       Entity entity = new Entity("activityparty");
       if (item.Value == Guid.Empty)  // item.value is the contact Id
       {
        entity["addressused"] = item.Key;
       }
      else
      {
        entity["partyid"] = new EntityReference("contact", item.Value);
      }
      recipientCollection.Entities.Add(entity);
    }
    currentEntity["to"] = recipientCollection;

}

*This post is locked for comments

I have the same question (0)
  • Gopalan Bhuvanesh Profile Picture
    11,401 on at

    Hi

    Is it

    cannot update activity when email is in draft state

    or

    cannot update activity when email is not in draft state

  • Verified answer
    Community Member Profile Picture
    on at

    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 !!!

  • Gopalan Bhuvanesh Profile Picture
    11,401 on at

    Mark your answer as verified so that it would help others to solve similar issue.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans