1
我想创建一个插件,根据电子邮件正文中可找到的特定格式创建一个记录。例如:c# - CRM 2011收到电子邮件时创建记录
PO/Dustine/Tolete/8:45 PM /样本定位/样品递减
到目前为止,我有这样的代码:
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);
}
}
}
但每次事件被触发时,一错误正在发生。我究竟做错了什么?
也许你可以给我们一个关于错误的提示? –
它只是说我在Outlook上跟踪它时发生错误。 –
尝试打开跟踪并收集错误。 – Piyush