CRM Dynamics插件错误:给定的键不存在于字典中(当我触发PhoneCall创建插件时)
本文关键字:插件 Dynamics 创建 PhoneCall 字典 CRM 于字典 不存在 错误 | 更新日期: 2023-09-27 18:02:07
当我在CRm Dynamics 2015中启动电话创建记录插件时,我得到以下错误,
public void Execute(IServiceProvider){tracingService =(ITracingService) serviceProvider.GetService (typeof (ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (context.MessageName == "Create")
{
try
{
Entity phoneCall = new Entity("phonecall");
Int64 NumberToCall = (Int64)phoneCall.Attributes["new_identity"];
Int64 ReceiveCallOn = (Int64)phoneCall.Attributes["new_destination"];
var apiKey = phoneCall.Attributes["new_apikey"].ToString();
Int64 fId = (Int64)phoneCall.Attributes["new_fid"];
Guid phoneResponse = service.Create(phoneCall);
BasicHttpBinding binding = new BasicHttpBinding();
binding.Name = "BasicHttpBinding_IService1";
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.MessageEncoding = WSMessageEncoding.Text;
binding.TransferMode = TransferMode.Buffered;
binding.UseDefaultWebProxy = true;
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
binding.SendTimeout = new TimeSpan(0, 10, 0);
EndpointAddress endPointAddress = new EndpointAddress("http://localhost:62009/Service1.svc");
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client(binding, endPointAddress);
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
client.ChannelFactory.Credentials.Windows.ClientCredential = new System.Net.NetworkCredential("MSCRM''Thabiso", "1pft?MG6bscu?g", "MSCRM0");
client.WebCall(NumberToCall, ReceiveCallOn, apiKey, fId);
}
catch (FaultException<OrganizationServiceFault> ex)
{
tracingService.Trace("MyPlugin: {0}", ex.ToString());
throw;
}
}
我相信答案是你的phoneCall记录不包含你所指的字段之一。实际上它不包含任何值。因为你实例化了一个实体的新实例,所以字段值不会凭空出现…我建议删除
行Entity phoneCall = new Entity("phonecall");
用entity替换phoneCall
Int64 NumberToCall = (Int64)phoneCall.Attributes["new_identity"];
Int64 ReceiveCallOn = (Int64)phoneCall.Attributes["new_destination"];
var apiKey = phoneCall.Attributes["new_apikey"].ToString();
Int64 fId = (Int64)phoneCall.Attributes["new_fid"];
不确定为什么要在…之后创建另一个电话。你能解释一下你的场景吗?
我相信您要做的是从正在创建的电话记录中获取属性。如果是这种情况,那么代码应该是这样的:
Int64 NumberToCall = entity.Attributes.Contains("new_identity")?(Int64)entity.Attributes["new_identity"]:0;
Int64 ReceiveCallOn = entity.Attributes.Contains("new_destination")?(Int64)entity.Attributes["new_destination"]:0;
var apiKey = entity.Attributes.Contains("new_apikey")?entity.Attributes["new_apikey"].ToString():0;
Int64 fId = entity.Attributes.Contains("new_fid")?(Int64)entity.Attributes["new_fid"]:0;
我的意思是说,而不是"电话"使用"实体",你从上下文中分配"目标"。在使用该属性之前,还要尝试检查该属性在实体中是否可用,以避免此类错误。
如果该属性不存在,请用您想要的默认值替换。