Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

Posted on by Microsoft Employee

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

  • Gee Bee Profile Picture
    Gee Bee 11,397 on at
    RE: Update Email Activity (Field type: Party List) using asynchronous plugin.

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

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Update Email Activity (Field type: Party List) using asynchronous plugin.

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

  • Gee Bee Profile Picture
    Gee Bee 11,397 on at
    RE: Update Email Activity (Field type: Party List) using asynchronous plugin.

    Hi

    Is it

    cannot update activity when email is in draft state

    or

    cannot update activity when email is not in draft state

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans