-1

I get the following error when I fire my plugin on phoncall create record in CRm Dynamics 2015,

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;

                }
            }
Papi
  • 555
  • 13
  • 40
  • possible duplicate of [The given key is not present in the dictionary](http://stackoverflow.com/questions/29798337/the-given-key-is-not-present-in-the-dictionary) – Sxntk Jul 09 '15 at 19:49

2 Answers2

1

I believe that answer is that your phoneCall record doesn't contain one of fields you are referring to. And actually it doesn't contain any values... because you instantiate new instance of a Entity so field values could not appear from nowhere... I would suggest to delete line

Entity phoneCall = new Entity("phonecall");

And replace phoneCall with entity in lines:

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

Not sure why you want to create another one phonecall after... Could you please explai your scenario?

Andrew Butenko
  • 5,048
  • 1
  • 14
  • 13
-1

I believe what you are trying to do is to get the attributes from the phone call record which is getting created. If that is the case then the code should be something like this.

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;

I meant to say instead of "phonecall" use "entity" where you are assigning the "Target" from the context. Also try to check whether the attribute is available in the entity or not before using it to avoid such errors.

Please substitute with the default values which you would like to have in case the attribute is not present.

Renjith
  • 755
  • 4
  • 7
  • I tried this approach where I check for , I get the error now saying specified cast is not valid, this is on the number to call attribute. This line here: - Int64 NumberToCall = entity.Attributes.Contains("new_identity")?(Int64)entity.Attributes["new_identity"]:0; – Papi Jul 14 '15 at 09:44
  • What is the data coming out of attribute new_identity? Check that, system is not able to convert that data to integer which is causing the problem. According to the expected data change the data type or vice versa. – Renjith Jul 16 '15 at 06:25