Answered by:
Resetting incoming emails to active

Question
-
Hi, my problem is to reset incoming emails to "active" when they arrive in the queue. There are several posts that touch on this but they are at quite high level and I would benefit from some detail.
I am using a plug-in to execute on message "DeliverIncoming" for email. Inside the plugin I am trying to call SetStateDynamicEntityRequest to reset the email.
I can see the plugin is being called from the outer trace message but the if condition is not being met. If anyone can point out what I'm doing wrong or suggest the right approach I'd be grateful. My code is:
public override void Execute()
{TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "Inside execute...");
if (Context.InputParameters.Properties.Contains("Target") &&
Context.InputParameters.Properties["Target"] is DynamicEntity)
{TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "Inside if statement...");
Moniker moniker = (Moniker)Context.InputParameters.Properties["Target"];
CrmServiceAdapter service = CrmServiceAdapter.New();
SetStateDynamicEntityRequest stateChangeRequest = new SetStateDynamicEntityRequest();
stateChangeRequest.Entity = moniker;
stateChangeRequest.State = "active";
stateChangeRequest.Status = -1;
service.Execute(stateChangeRequest);
}}
Bob BazelyTuesday, October 11, 2011 11:24 AM
Answers
-
Hello Bob,
Yes you should handle Create message in child pipeline. To validate that Create was called from DeliverIncoming you can use following validation inside your plugin:
if (context.MessageName == MessageName.Create && context.InputParameters.Contains(ParameterName.Target) && context.InputParameters[ParameterName.Target] is DynamicEntity && context.ParentContext != null && context.ParentContext.MessageName == MessageName.DeliverIncoming) { //your logic here }
Microsoft CRM Freelancer
My blog (english)
Мой блог (русскоязычный)
- Marked as answer by Bob Bazely Friday, October 14, 2011 8:55 AM
Wednesday, October 12, 2011 8:25 AMModerator
All replies
-
Hi,
Try use SetStateEmailRequest to change the email status.
Jehanzeb Javeed
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".Tuesday, October 11, 2011 11:39 AM -
Thanks. How do I obtain the EntityId inside the plug-in?
Bob BazelyTuesday, October 11, 2011 12:05 PM -
Hi,
You can get entity Id via the following code:
Guid EntityId = moniker.Id;
Jehanzeb Javeed
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Proposed as answer by Jehanzeb.Javeed Tuesday, October 11, 2011 12:07 PM
Tuesday, October 11, 2011 12:07 PM -
Hi - the plug-in doesn't seem to se the incoming email as a moniker (or a dynamicEntity). If I run the code below the trace messages tell me that the DynamicEntity tests is false and the Moniker test is false. What should I be testing for?
TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "EmailDeliverIncoming execute method starting..."
);
if (Context.InputParameters.Properties.Contains("Target"
))
{
TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "EmailDeliverIncoming contains Target test true..."
);
if (Context.InputParameters.Properties["Target"] is DynamicEntity
)
{
TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "EmailDeliverIncoming DynamicEntity test true..."
);
}
else
{
TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "EmailDeliverIncoming DynamicEntity test false..."
);
}
}
else
{
TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "EmailDeliverIncoming contains Target test false..."
);
}
if (Context.InputParameters.Properties.Contains("EntityMoniker"
))
{
TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "EmailDeliverIncoming contains EntityMoniker test true..."
);
if (Context.InputParameters.Properties["EntityMoniker"] is Moniker
)
{
TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "EmailDeliverIncoming Moniker test true..."
);
}
else
{
TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "EmailDeliverIncoming Moniker test false..."
);
}
}
else
{
TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "EmailDeliverIncoming contains EntityMoniker test false..."
);
}
Bob BazelyTuesday, October 11, 2011 12:20 PM -
Hi,
On Email entity Create message you will not get the EntityMoniker but you will get the DynamicEntity object, try the following code or debug (http://msdn.microsoft.com/en-us/library/cc151088.aspx) the plugin below to get the object type to make it clear:
throw new InvalidPluginExecutionException(Context.InputParameters.Properties["Target"].GetType().ToString());
Jehanzeb Javeed
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".Tuesday, October 11, 2011 12:31 PM -
Thanks for your time on this Jehanzeb, I'll try that.
One question, you mention the create message - should my plug-in execute against the create message? I have been using the DeliverIncoming message.
Bob BazelyTuesday, October 11, 2011 12:37 PM -
Hi,
Yes try Create message in Post-Operation stage instead of DeliverIncomming message.
Jehanzeb Javeed
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Proposed as answer by Jehanzeb.Javeed Tuesday, October 11, 2011 12:47 PM
Tuesday, October 11, 2011 12:47 PM -
thanks, I'll try that
Bob BazelyTuesday, October 11, 2011 12:55 PM -
Can't get this working with the Create message, do you mean the Create in the child pipeline of DeliverIncoming? How would I capture that?
Bob BazelyWednesday, October 12, 2011 8:13 AM -
Hi,
Have you registered your plugin on Create message for Email entity in Pre-Event or Post-Event stage in Parent Pipeline ? Create event will always fire whever a new email record will be created, in you plugin code you may need to check if the email direction is incomming then only execute your code and on Create message you will recieve the DynamicEntity object in Target parameter.
Deliver incomming message is used to route the e-mail to the appropriate set of system users or contacts, you should better use Create message
Jehanzeb Javeed
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".Wednesday, October 12, 2011 8:22 AM -
Hello Bob,
Yes you should handle Create message in child pipeline. To validate that Create was called from DeliverIncoming you can use following validation inside your plugin:
if (context.MessageName == MessageName.Create && context.InputParameters.Contains(ParameterName.Target) && context.InputParameters[ParameterName.Target] is DynamicEntity && context.ParentContext != null && context.ParentContext.MessageName == MessageName.DeliverIncoming) { //your logic here }
Microsoft CRM Freelancer
My blog (english)
Мой блог (русскоязычный)
- Marked as answer by Bob Bazely Friday, October 14, 2011 8:55 AM
Wednesday, October 12, 2011 8:25 AMModerator -
Thanks both - I appreciate your time responding
OK I will try using the child pipeline
Bob BazelyWednesday, October 12, 2011 8:34 AM -
Hi, this is obviously the way to go I can see the plugin firing as the mail enters the queue, I am using the code below for the Create message in the child pipeline of the DeliverIncoming message.
I have a remaining problem where service.Execute() throws an exception:
<detail><error>
<code>0x80044150</code>
<description>Generic SQL error.</description>
<type>Platform</type>
</error></detail>This looks like it is a security issue maybe, but separate to the question I originally asked. Thanks again for your help.
public override void Execute() { TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "EmailDeliverIncomingPostCreate execute starting..."); if (Context.MessageName == MessageName.Create && Context.InputParameters.Contains(ParameterName.Target) && Context.InputParameters[ParameterName.Target] is DynamicEntity && Context.ParentContext != null && Context.ParentContext.MessageName == MessageName.DeliverIncoming) { TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "EmailDeliverIncomingPostCreate inside main if statement..."); DynamicEntity postEntity = (DynamicEntity)Context.PostEntityImages.Properties[this.PostEntityImageName]; if (postEntity.Properties.Contains("activityid")) { TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "EmailDeliverIncomingPostCreate inside activityid if statement..."); try { TraceHelper.LogMessage(TraceEntryType.Information, EventSource.Callout, "EmailDeliverIncomingPostCreate inside try..."); Guid entityId = ((Key)postEntity.Properties["activityid"]).Value; CrmServiceAdapter service = CrmServiceAdapter.NewImpersonatedConnection(); SetStateEmailRequest stateChangeRequest = new SetStateEmailRequest(); stateChangeRequest.EntityId = entityId; stateChangeRequest.EmailState = EmailState.Open; stateChangeRequest.EmailStatus = -1; service.Execute(stateChangeRequest); } catch (Exception e) { TraceHelper.LogMessage(TraceEntryType.Error, EventSource.Callout, "EmailDeliverIncomingPostCreate try failed with error" + e.Message); throw e; } } } }
Bob BazelyFriday, October 14, 2011 8:55 AM -
Hello,
I have faced with the same issue as you. Unfortunatelly the only way out is to switch to async mode. But in this case you would not get the possibility to get an access to parent context. The way out I have fount is following:
1. In this plugin create some marker (I used custom entity) and store identifier of email inside.
2. Create another plugin which set to work as async to handle those marker (in my case it was the plugin which handles create of custom entity instance in async mode).
Microsoft CRM Freelancer
My blog (english)
Мой блог (русскоязычный)
Friday, October 14, 2011 10:44 AMModerator -
Yes, I see. Thanks for this.
Bob BazelyFriday, October 14, 2011 3:44 PM