[CRM 2011] Problem changing the email recipient in a plugin RRS feed

  • Question

  • We would like to be able to send emails to something other than system users, accounts and contacts. Meaning, we have a custom entity containing an email address, and a workflow which is supposed to send a message to that address.

    Alas, the default mechanisms in workflows don't allow sending to anything BUT the standard entities, so I'm trying to write a plugin to change the email's recipient based on the type of "regarding entity".

    I am, however, struggling with actually changing the email itself!


    var email = ctx.EmailSet.Where(e => e.Id == localContext.PluginExecutionContext.PrimaryEntityId).Single();             var party = newEntity("activityparty");             party["addressused"] = "some@email.com";             email["to"] = newEntity[] { party };             ctx.UpdateObject(email);             ctx.SaveChanges();


    This is my experimental post-create code - my only goal is to change the email address of the recipient to "some@email.com". However, the whole thing crashes on SaveChanges without any specific error.

    I've also tried a pre-create code which did almost the same thing (email was taken from PluginExecutionContext.InputParameters["Target"]). The strange part was that the pre-create code didn't throw any exceptions, but no changes would be made.

    What's going on? How can I change that recipient?

    Wednesday, July 17, 2013 2:00 PM


All replies

  • Hi Bender,

    I have a question here. You said that the workflow is not allowing you to send email to your custom entity. My question: Is the custom entity is an emailable entity? Please verify in the Entity Customization that "Sending E-mail" option is set to "Yes" or "No". If it is "No" then please make it "Yes". After marking it "Yes" you should be able to send emails through workflow only.

    Please let me know your observation. 

    Please mark as Answer if it solves your requirement. 



    Thursday, July 18, 2013 7:02 AM
  • Yeah, it wasn't an emailable entity, and we've fixed that already. This specific use-case has, as such, been solved.

    However, I would still like to know why the code I posted didn't work!

    Thursday, July 18, 2013 7:07 AM
  • Hi Bender,

    I never tried this code. But as a CRM functionality ActivityParty can be any emailable entity and "addressused" will be the primary email field of the entity.

    I think we need to change the email address on the entity record and not the ActivityParty's addressused.

    If you update the entity email address then you can send the email.



    • Edited by Shaleenb Thursday, July 18, 2013 7:29 AM Typing mistake
    Thursday, July 18, 2013 7:26 AM
  • You're thinking about sending an email to an entity in CRM (which has an email property).

    But, CRM also allows you to send an email just by specifying an email address (it doesn't need to be from any entity in the system). If you create a new email and fill out the "To" field with a random email address, then the InputParameters will contain the following:

    - an email entity
    - the email will contain a "to" attribute, which will contain an EntityCollection
    - said collection will contain an activityparty entity
    - that activityparty entity will only have one attribute: addressused

    Changing the address used to another email (for the sake of argument, use an another random email which doesn't exist in CRM) inside the pre-operation step does not, in fact, change the recipient of the email. For comparison, if you change the subject of the email in the pre-operation step (email["subject"] = "some random other subject";) then this change will be saved.

    Thursday, July 18, 2013 7:32 AM
  • Please check below in System Settings (Settings->Administration->System Settings->Email Tab):

    Please make sure that "Allow messages with unresolved e-mail recipients to be sent" is set to true



    Thursday, July 18, 2013 9:48 AM
  • That setting only specifies if an email can actually be sent. My problem exists when trying to create an email activity, and seems to be a coding issue, not a CRM configuration issue.

    In other words - changing the setting doesn't resolve the issue.

    Thursday, July 18, 2013 9:56 AM
  • Hi,

    It will work with pre operation but not with post. did you tried??

    Friday, December 20, 2013 7:28 AM
  • If you're trying to modify any partylist attribute in a plugin, it will only work on stage=10 (before the transaction) - see http://mscrmuk.blogspot.co.uk/2011/07/partylist-attributes-and-plugin-event.html

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    • Marked as answer by Bender Mateusz Friday, December 20, 2013 1:01 PM
    Friday, December 20, 2013 12:25 PM
  • That might very well be it, David!

    Darn those undocumented features... *shakes fist*

    Friday, December 20, 2013 1:01 PM