电子邮件到达时CRM 2011更新事件

时间:2013-05-13 22:03:35

标签: c# plugins dynamics-crm-2011

在CRM中,当电子邮件到达并在其中包含跟踪令牌时,他们会自动将相关字段设置为事件(或与其相关的任何内容)

不幸的是,记录墙未使用此信息进行更新,因此即使您正在关注此案例,也不会提醒您注意新活动。

我想在电子邮件或事件(或两者)上编写一个插件,用于更新记录墙,并在3天内创建一个跟踪该电子邮件的任务。

我正在查看SDK,当电子邮件在到达CRM时设置了相关字段时,我无法看到管道中的相应事件是什么。

CRM电子邮件创建生命周期在文档中没有很好地描述。 [握拳]

困扰我的额外事情

我似乎无法包含引用强类型电子邮件,帖子或案例(让我发疯)

测试这个非常困难(比应该更难)

编辑这是我当前的代码

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;
        }
    }
}
}

4 个答案:

答案 0 :(得分:3)

我一直在与这个完全相同的问题进行斗争,并且发现了这篇文章。我以为我会为你发布解决方案(如果你还需要它)以及将来遇到这个问题的其他人。

这是我到达的解决方案:   - 使用插件注册工具在适当的步骤注册新图像(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;

希望这有帮助!

答案 1 :(得分:1)

我现在正在制作一个非常相似的插件。我在收到发往某个电子邮件地址的电子邮件后创建了一个自定义实体。它还通过“关注”字段将传入的电子邮件与该新记录相关联。我在“创建电子邮件”中添加了“操作前”步骤,它运行良好,包括来自路由器的传入电子邮件。

当CRM填写About字段时,我不确定是什么。你可能会看一下Post-Operation,看看它是否设置在那里?

关于关注字段的一个有趣的警告(哈哈!):与单个查找字段不同,关注对象的名称实际上存储在ActivityPointer表中,因此当您更新关注字段时,请务必在EntityReference上设置名称。如果不这样做,则“查找”仍然会有一个可点击的图标,但不会有任何文本。我是这样做的:

        email.RegardingObjectId = [yourentity].ToEntityReference();
        email.RegardingObjectId.Name = email.Subject;

希望有所帮助!

答案 2 :(得分:1)

我最终在电子邮件实体的工作流程中执行此操作

步骤

  1. 创建新工作流程,我将其称为“传入电子邮件工作流程”
  2. 范围是组织
  3. 选择电子邮件作为实体,然后选中“记录字段更改”
  4. 添加检查关于(案例):案例包含数据的步骤
  5. 如果是的话:

    1. 添加创建帖子的步骤
    2. 编辑帖子

      中的属性
      • 文字:此案例来自{来自(电子邮件)}
      • 的{方向(电子邮件)}电子邮件活动
      • 来源:自动发布
      • 关于:{关于(电子邮件)}
    3. 添加创建任务的步骤

    4. 在“任务”中编辑属性
      • 主题:跟进{主题(电子邮件)}
      • 关于:{关于(电子邮件)}

答案 3 :(得分:0)

尝试使用以下代码:

if ((bool)entity["directioncode"] == false)

而不是你的代码:

if((string)target["direction"] != "Incoming")