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