c# - CRM 2011在收到电子邮件时创建记录

时间:2013-11-28 03:40:39

标签: c# dynamics-crm-2011 dynamics-crm crm

我想创建一个插件,根据可以在电子邮件正文中找到的特定格式创建记录。例如:

PO / Dustine / Tolete / 8:45 PM /样品位置/样品desc

到目前为止,我有这段代码:

using System;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Xrm;

public class Plugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        IPluginExecutionContext context = (IPluginExecutionContext)
        serviceProvider.GetService(typeof(IPluginExecutionContext));

        Entity entity;

        // Check if the input parameters property bag contains a target
        // of the create operation and that target is of type Entity.
        if (context.InputParameters.Contains("Target") &&
        context.InputParameters["Target"] is Entity)
        {
            // Obtain the target business entity from the input parameters.
            entity = (Entity)context.InputParameters["Target"];

            // Verify that the entity represents a contact.
            if (entity.LogicalName != "email") { return; }
        }
        else
        {
            return;
        }

        try
        {
            IOrganizationServiceFactory serviceFactory =
                (IOrganizationServiceFactory)serviceProvider.GetService(
            typeof(IOrganizationServiceFactory));
            IOrganizationService service =
            serviceFactory.CreateOrganizationService(context.UserId);

            var id = (Guid)context.OutputParameters["id"];

            AddNewServiceRequest(service, id);
        }
        catch (FaultException<OrganizationServiceFault> ex)
        {
            throw new InvalidPluginExecutionException(
            "An error occurred in the plug-in.", ex);
        }
    }

    private static void AddNewServiceRequest(IOrganizationService service, Guid id)
    {
        using (var crm = new XrmServiceContext(service))
        {

            var email = crm.EmailSet.Where(c => c.ActivityId == id).First();

            string[] noteText = email.Description.ToString().Split('/');

            foreach(string text in noteText){

                Console.WriteLine(text);
            }

            Entity peaceAndOrder = new Entity("msa_noisecomplaintrequest");

            peaceAndOrder["msa_firstname"] = noteText[1];
            peaceAndOrder["msa_lastname"] = noteText[2];
            peaceAndOrder["msa_incidenttime"] = noteText[3];
            peaceAndOrder["msa_location"] = noteText[4];
            peaceAndOrder["msa_description"] = noteText[5];

            service.Create(peaceAndOrder);
        }
    }
}

但每次触发事件时,都会发生错误。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您正在使用context.OutputParameters来获取电子邮件ID。我假设你的插件是在post创建事件上注册的。 确保步骤确实已正确注册(即在post事件上运行),并且电子邮件实体上没有运行其他插件。

如果你确实有其他插件在电子邮件上运行(例如预事件插件),你必须用适当的条件包装你的代码,确保只在post创建事件上运行,即

if (context.Stage == 40 /*Post Operation*/) 
{
  //  Your code here …
}

使用检查正确消息名称的条件包装代码也是一种好习惯,例如

if (context.MessageName == "CREATE")
{
   //  Your code here …
}

作为旁注,您将AddNewServiceRequest定义为静态。插件由CRM缓存,因此它们在某种意义上是静态的。您不需要声明静态成员 在您的代码中,除非您打算在插件之间或在对象内声明静态成员时共享数据。

最后,启用平台跟踪或调试以瞥见真正导致此问题的原因。