CRM Dynamics插件错误:字典中没有给定的密钥(当我在PhoneCall Create上触发插件时)

时间:2015-07-09 16:36:10

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

当我在CRm Dynamics 2015中的phoncall创建记录中触发我的插件时出现以下错误,

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

                }
            }

2 个答案:

答案 0 :(得分:1)

我认为答案是您的phoneCall记录不包含您所指的字段之一。实际上它并没有包含任何值...因为你实例化了一个实体的新实例所以字段值无法从无处出现...我建议删除行

from tkinter import *

class comd: # Contains primary commands
    # Capital Rule ----------------------------
    # G = Get | I = Insert | D = Draw | S = Set
    # -----------------------------------------

    def Ggeo(self): # Get Geometry (Get window geometry)
        x = root.winfo_width()
        y = root.winfo_height()
        print("Current Window Geometry")
        print(str(x) + " x " +str(y))

    def Idum(self): # Insters "Dummy Insert"
        import tkinter as tkin
        tbox.insert(INSERT, "Dummy Insert")

    def Ilim(self): # Prints How many lines are in
        info =  int(tbox.index('end-1c').split('.')[0])
        print(info)



root = Tk()
root.geometry("885x600-25-25")

tbox = Text(root, font=("Courier","14","bold"))
tbox.pack(expand = True , fill = BOTH)


# Problem here --------------------
tbox.bind("<Control-i>", comd.Ilim)
# ---------------------------------


mainloop()

用行中的实体替换phoneCall:

Entity phoneCall = new Entity("phonecall");

不确定为什么要在...后创建另一个电话 你能解释一下你的情景吗?

答案 1 :(得分:-1)

我相信你要做的就是从正在创建的电话记录中获取属性。如果是这种情况,则代码应该是这样的。

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;

我的意思是说而不是&#34; phonecall&#34;使用&#34;实体&#34;在哪里指定&#34;目标&#34;从上下文。在使用该属性之前,还要尝试检查该属性是否可用,以避免此类错误。

如果属性不存在,请使用您希望拥有的默认值。