【问题标题】:CRM Dynamics Plugin Error : The given key was not present in the dictionary (When I trigger plugin on PhoneCall Create)CRM Dynamics 插件错误:字典中不存在给定的键(当我在 PhoneCall Create 上触发插件时)
【发布时间】:2015-07-09 16:36:10
【问题描述】:

当我在 CRM Dynamics 2015 中的 phoncall 创建记录上启动插件时,我收到以下错误,

public void Execute(IServiceProvider serviceProvider) { ITracingService trackingService = (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;

                }
            }

【问题讨论】:

标签: c# plugins crm dynamics-crm-2013 dynamics-crm-2015


【解决方案1】:

我相信答案是您的电话记录不包含您所指的字段之一。实际上它不包含任何值...因为您实例化了实体的新实例,因此字段值无法从任何地方出现...我建议删除行

Entity phoneCall = new Entity("phonecall");

并将 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"];

不知道为什么要在...之后再创建一个电话 你能解释一下你的情况吗?

【讨论】:

    【解决方案2】:

    我相信您要做的是从正在创建的电话记录中获取属性。如果是这样,那么代码应该是这样的。

    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;
    

    我的意思是说,在您从上下文中分配“目标”的地方,不要使用“电话”,而是使用“实体”。在使用它之前还要尝试检查该属性在实体中是否可用以避免此类错误。

    如果属性不存在,请替换为您希望使用的默认值。

    【讨论】:

    • 我尝试了这种方法来检查,我现在收到错误消息,说指定的演员表无效,这是要调用属性的号码。这一行在这里: - Int64 NumberToCall = entity.Attributes.Contains("new_identity")?(Int64)entity.Attributes["new_identity"]:0;
    • 属性 new_identity 输出的数据是什么?检查是否系统无法将该数据转换为导致问题的整数。根据预期的数据改变数据类型,反之亦然。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多