C# CRM-如何为更新消息创建插件
因此,我创建了一个插件,基于微软在。它实际上是在createmessage上,所以我修改了一些代码并在updatemessage下注册了它 但当我在更新时注册它时,会引发一个异常,该异常表示:C# CRM-如何为更新消息创建插件,c#,plugins,crm,C#,Plugins,Crm,因此,我创建了一个插件,基于微软在。它实际上是在createmessage上,所以我修改了一些代码并在updatemessage下注册了它 但当我在更新时注册它时,会引发一个异常,该异常表示: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. 所以我试着在CreateMessage下注册它,它成功了 但我需要在update下注册它,而且我找不到一个可
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
所以我试着在CreateMessage下注册它,它成功了
但我需要在update下注册它,而且我找不到一个可靠的源代码来说明如何更改我的代码以便update能够处理它。这是我的密码:
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;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "scs_barangayclearanceapplication") { return; }
}
else
{
return;
}
try
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//var id = (Guid)context.OutputParameters["id"];
SendEmailToContact(service, entity.Id);
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException(
"An error occurred in the plug-in.", ex);
}
}
private static void SendEmailToContact(IOrganizationService service, Guid id)
{
using (var crm = new XrmServiceContext(service))
{
var testData = crm.scs_barangayclearanceapplicationSet.Where(b => b.Id == id).First();
string sr = testData.scs_ServiceRequestNumber;
var note = new Annotation
{
Subject = "Created with plugin",
NoteText = " Service Request ID = " + id + " Service Request Number = " + sr,
ObjectId = testData.ToEntityReference(),
ObjectTypeCode = testData.LogicalName
};
crm.AddObject(note);
crm.SaveChanges();
}
}
}
使用系统;
使用系统诊断;
使用System.Linq;
使用System.ServiceModel;
使用Microsoft.Xrm.Sdk;
使用Xrm;
公共类插件:IPlugin
{
public void Execute(IServiceProvider服务提供程序)
{
IPluginExecutionContext上下文=(IPluginExecutionContext)serviceProvider.GetService(类型为(IPluginExecutionContext));
实体;
if(context.InputParameters.Contains(“目标”)&&context.InputParameters[“目标”]是实体)
{
实体=(实体)上下文.InputParameters[“目标”];
if(entity.LogicalName!=“scs_barangayclearanceapplication”){return;}
}
其他的
{
回来
}
尝试
{
IOOrganizationServiceFactory serviceFactory=(IOOrganizationServiceFactory)serviceProvider.GetService(类型为(IOOrganizationServiceFactory));
IOOrganizationService=serviceFactory.CreateOrganizationService(context.UserId);
//var id=(Guid)context.OutputParameters[“id”];
SendEmailToContact(服务,entity.Id);
}
捕获(FaultException-ex)
{
抛出新的InvalidPlugineExecutionException(
“插件中发生错误。”,ex);
}
}
私有静态无效SendEmailToContact(IOOrganizationService服务,Guid id)
{
使用(var crm=new XrmServiceContext(服务))
{
var testData=crm.scs_barangaycelaranceapplicationstart.Where(b=>b.Id==Id.First();
字符串sr=testData.scs\u ServiceRequestNumber;
var note=新注释
{
Subject=“使用插件创建”,
NoteText=“服务请求ID=“+ID+”服务请求编号=“+sr,
ObjectId=testData.ToEntityReference(),
ObjectTypeCode=testData.LogicalName
};
crm.AddObject(注);
SaveChanges();
}
}
}
我真的需要你的帮助。谢谢 你有具体的问题吗?在创建/更新时触发的唯一真正区别是您可以访问哪些属性…抱歉,没有阅读问题…错误表明您正在尝试访问不存在的属性。你能解释一下这句话吗
var testData=crm.scs_barangayclaranceapplicationstart.Where(b=>b.Id==Id.First()代码>您也是在注册pre还是post?