2
votes

I'm trying to create plugin that fires when an email is sent.

At first I tried using SetState and SetStateDynamicEntity to when the status is changed to completed. However the plugin never gets fired when an email is sent.

So I tried using the "Send" message using OnExecute method. Is it possible to retrieve the sent email entity info? I'm stumped in trying to return the Entity. The code below keeps returning "The given key was not present in the dictionary" error message.

I tried using either "Entity" or "EntityReference" but no luck.

I know that the Send message returns the EmailId. Is the only way to return the Entity from this EmailId?

public override void OnExecute(IServiceProvider serviceProvider, IPluginExecutionContext context) { var trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

        if (!context.InputParameters.Contains("EmailId"))
        {
            return;               
        }

        var emailId = (Guid)context.InputParameters["EmailId"];

        Entity emailEntity = (Entity)context.InputParameters["Target"];
1
I just use retrieve to get the entity but wondering if there's a more direct way. Entity emailEntity = service.Retrieve("email", emailId, new ColumnSet(true));ichachan
Hi ichachan - what Plugin message/step did you register to? I'm also in a similar boat. Not sure what Plugin event indicates the email has been sent.Don Cheadle

1 Answers

2
votes

So the way you mentioned is the only correct way to get email records. Totaling up:

  1. Retrieve identifier of email from context.
  2. Retrieve email record using crm endpoint.

Following article contains description that is relevant to CRM 4.0 - http://www.patrickverbeeten.com/Blog/2008/01/25/CRM-40-Plug-in-message-input-parameters haven't seen something similar that was done for CRM 2011/2013/2015/2016.