c# - 电子邮件到达时的 CRM 2011 更新事件

标签 c# plugins dynamics-crm-2011

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

不幸的是,记录墙没有更新此信息,因此即使您正在关注此案例,也不会提醒您有新事件。

我想写一个关于电子邮件或事件(或两者)的插件来更新记录墙并创建一个任务以在 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;
        }
    }
}
}

最佳答案

我一直在与这个完全相同的问题作斗争,并偶然发现了这篇文章。我想我会为您(如果您仍然需要)和将来遇到此问题的任何其他人发布解决方案。

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

希望这对您有所帮助!

关于c# - 电子邮件到达时的 CRM 2011 更新事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16531926/

相关文章:

Cordova:在插件中添加库

c++ - QtPlugins 实现多个接口(interface)并转换为通用接口(interface)

.net - 绕过成员名称不能与其封闭类型相同

dynamics-crm-2011 - 当实体在 crm 2011 中打开时触发插件

c# - 尝试使用 Autodesk.AutoCAD.DatabaseService 从 DWG 中读取数据 我需要引用什么 DLL?

c# - 线程是在 C# 用户级还是内核级创建的?

c# - 我正在尝试使用 FileStream 创建文件但出现异常,为什么?

c# - Windows 窗体未处理异常对话框

plugins - Notepad++ 将鼠标悬停在链接或颜色上预览?

c# - 如何使用 CamlQuery 按名称获取共享点文件夹