邮件到达时发生CRM 2011更新事件
本文关键字:CRM 2011 更新 事件 | 更新日期: 2023-09-27 18:13:18
在CRM中,当电子邮件到达并且其中有跟踪令牌时,它们会自动将有关字段设置为事件(或与之相关的任何内容)
不幸的是,记录墙没有更新此信息,所以即使您正在跟踪该情况,也不会提醒您新的活动。
我想在电子邮件或事件(或两者)上写一个插件,更新记录墙并创建一个任务,以便在3天内跟进该电子邮件。
我正在查看SDK,但我看不出管道中适当的事件是什么,以便在电子邮件到达CRM时设置其有关字段。
文档中没有很好地描述CRM电子邮件创建生命周期。(摇动的拳头]
让我烦恼的额外的事情
我似乎不能包含一个引用来获取强类型的Email、Post或Case(快把我逼疯了)
测试这个真的很难(比它应该的更难)
EDIT这是我当前的代码
namespace Assembly.Plugins
{
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
/// <summary>
/// PostEmailDeliverIncoming Plugin.
/// </summary>
public class PostEmailDeliverIncoming : Plugin
{
/// <summary>
/// Initializes a new instance of the <see cref="PostEmailDeliverIncoming"/> class.
/// </summary>
public PostEmailDeliverIncoming()
: base(typeof(PostEmailDeliverIncoming))
{
RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "DeliverIncoming", "email", ExecutePostEmailDeliverIncoming));
// Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
// You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
}
protected void ExecutePostEmailDeliverIncoming(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService = localContext.TracingService;
// Obtain the execution context from the service provider.
IPluginExecutionContext context = localContext.PluginExecutionContext;
// Obtain the organization service reference.
var service = localContext.OrganizationService;
// The InputParameters collection contains all the data passed in the message request.
if (!context.InputParameters.Contains("Target") || !(context.InputParameters["Target"] is Entity))
return;
// Obtain the target entity from the input parmameters.
var target = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
if (target.LogicalName != "email")
return;
if((string)target["direction"] != "Incoming")
return;
if (target["regardingobjectid"] == null)
return;
try
{
// if its not a case I don't care
var incident = service.Retrieve("incident", (Guid)target["regardingobjectid"], new ColumnSet(true));
if (incident == null)
return;
var post = new Entity("post");
post["regardingobjectid"] = target["regardingobjectid"];
post["source"]=new OptionSetValue(0);
post["text"] = String.Format("a new email has arrived.");
// Create the task in Microsoft Dynamics CRM.
tracingService.Trace("FollowupPlugin: Creating the post.");
service.Create(post);
// Create a task activity to follow up with the account customer in 7 days.
var followup = new Entity("task");
followup["subject"] = "Follow up incoming email.";
followup["description"] = "An email arrived that was assigned to a case please follow it up.";
followup["scheduledstart"] = DateTime.Now.AddDays(3);
followup["scheduledend"] = DateTime.Now.AddDays(3);
followup["category"] = context.PrimaryEntityName;
// Refer to the email in the task activity.
if (context.OutputParameters.Contains("id"))
{
var regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
followup["regardingobjectid"] = new EntityReference("email", regardingobjectid);
}
// Create the task in Microsoft Dynamics CRM.
tracingService.Trace("FollowupPlugin: Creating the task activity.");
service.Create(followup);
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in the FollupupPlugin plug-in.", ex);
}
catch (Exception ex)
{
tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
throw;
}
}
}
}
我一直在与这个完全相同的问题作斗争,然后看到了这篇文章。我想我应该为你(如果你仍然需要它)和其他任何人在未来遇到这个问题的解决方案。
这是我得到的解决方案:-使用插件注册工具在适当的步骤上注册一个新图像(Stage = "40", MessageName = "DeliverIncoming")—将"新建镜像"设置为"后置镜像"-在你的插件中获取Post Image的实体ID:
Guid emailID = context.PostEntityImages["PostImage"].Id;
Entity emailFromRetrieve = localContext.OrganizationService.Retrieve(
"email",
emailID,
new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
Email email = emailFromRetrieve.ToEntity<Email>();
if (email.RegardingObjectId == null)
{
return;
}
var regardingObject = email.RegardingObjectId;
希望这对你有帮助!
我现在正在做一个非常类似的插件。我的程序在收到发送到某个电子邮件地址的电子邮件时创建一个自定义实体。它还通过concerning字段将传入的电子邮件与新记录关联起来。我在创建电子邮件上添加了一个操作前步骤,它工作得很好,包括从路由器收到的电子邮件。
我不确定的是CRM何时填写关于字段。你可以看看Post-Operation,看看它是否设置在那里?
关于concerning字段的一个有趣的警告(哈哈!):与单个查找字段不同,concerning对象的名称实际上存储在ActivityPointer表中,因此当您更新concerning字段时,请确保在EntityReference上设置name。如果不这样做,关于查找仍然会有一个可点击的图标,但不会有任何文本。我是这样做的:
email.RegardingObjectId = [yourentity].ToEntityReference();
email.RegardingObjectId.Name = email.Subject;
希望有帮助!
我最终在电子邮件实体的工作流中这样做了
步骤- 创建新的工作流,我将其命名为"incoming email workflow"
- 范围为组织
- 选择Email作为实体并勾选"记录字段更改"
- 增加一个步骤,检查关于(Case):Case Contains Data
如果属实:
- 添加创建Post的步骤
编辑Post属性
- 文本:此案例有来自{from (E-mail)}的{Direction(E-mail)}电子邮件活动
- 来源:Auto Post
- 关于:{关于(E-mail)}
添加创建任务的步骤
编辑任务中的属性- 主题:Follow up {Subject(E-mail)}
- 关于:{关于(E-mail)}
尝试使用以下代码:
if ((bool)entity["directioncode"] == false)
代替你的代码:
if((string)target["direction"] != "Incoming")