title: The given key was not present in the dictionary Microsoft CRM Plugin
body: <p>Another Microsoft CRM plugin related question. My plugin is firing when an invoice record is updated. I want to copy the contents of some of the invoice fields to the corresponding fields on the commission record. The if which contains the due date seems to work ok but the if containing the invoiceamount gives a key not present error. I have checked that all the field names are correct and exist in the correct entities.</p>
  
  <p>Any ideas what's happening? Do I need to use images and if so, could somebody help me with some code, please?</p>
  
  <p>Thanks</p>
  
  <pre><code>// &lt;copyright file="PreInvoiceUpdate.cs" company=""&gt;
  // Copyright (c) 2013 All Rights Reserved
  // &lt;/copyright&gt;
  // &lt;author&gt;&lt;/author&gt;
  // &lt;date&gt;8/8/2013 10:40:22 AM&lt;/date&gt;
  // &lt;summary&gt;Implements the PreInvoiceUpdate Plugin.&lt;/summary&gt;
  // &lt;auto-generated&gt;
  //     This code was generated by a tool.
  //     Runtime Version:4.0.30319.1
  // &lt;/auto-generated&gt;
  namespace UpdateCommission
  {
      using System;
      using System.ServiceModel;
      using Microsoft.Xrm.Sdk;
      using Microsoft.Xrm.Sdk.Query;
  
  
      /// &lt;summary&gt;
      /// PreInvoiceUpdate Plugin.
      /// Fires when the following attributes are updated:
      /// All Attributes
      /// &lt;/summary&gt;    
      public class PreInvoiceUpdate : Plugin
      {
          /// &lt;summary&gt;
          /// Initializes a new instance of the &lt;see cref="PreInvoiceUpdate"/&gt; class.
          /// &lt;/summary&gt;
          public PreInvoiceUpdate()
              : base(typeof(PreInvoiceUpdate))
          {
              base.RegisteredEvents.Add(new Tuple&lt;int, string, string, Action&lt;LocalPluginContext&gt;&gt;(20, "Update", "invoice", new Action&lt;LocalPluginContext&gt;(ExecutePreInvoiceUpdate)));
  
              // Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
              // You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
          }
  
          /// &lt;summary&gt;
          /// Executes the plug-in.
          /// &lt;/summary&gt;
          /// &lt;param name="localContext"&gt;The &lt;see cref="LocalPluginContext"/&gt; which contains the
          /// &lt;see cref="IPluginExecutionContext"/&gt;,
          /// &lt;see cref="IOrganizationService"/&gt;
          /// and &lt;see cref="ITracingService"/&gt;
          /// &lt;/param&gt;
          /// &lt;remarks&gt;
          /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
          /// The plug-in's Execute method should be written to be stateless as the constructor
          /// is not called for every invocation of the plug-in. Also, multiple system threads
          /// could execute the plug-in at the same time. All per invocation state information
          /// is stored in the context. This means that you should not use global variables in plug-ins.
          /// &lt;/remarks&gt;
          protected void ExecutePreInvoiceUpdate(LocalPluginContext localContext)
          {
              if (localContext == null)
              {
                  throw new ArgumentNullException("localContext");
              }
  
              // TODO: Implement your custom Plug-in business logic.
  
              // Obtain the execution context from the service provider.
              IPluginExecutionContext context = localContext.PluginExecutionContext;
              IOrganizationService service = localContext.OrganizationService;
              IServiceProvider serviceProvider = localContext.ServiceProvider;
              ITracingService tracingService = localContext.TracingService;
  
  
              // Obtain the target entity from the input parmameters.
              //Entity contextEntity = (Entity)context.InputParameters["Target"];
  
              if (context.Depth == 1)
              {
  
                  #region Set up variables
  
                  Entity targetInvoice = null;
                  targetInvoice = (Entity)context.InputParameters["Target"];
                  Guid invoiceID = targetInvoice.Id;
  
  
                  ColumnSet invoiceCols = new ColumnSet("new_totalcomm", "new_grossprofit", "new_invoiceamount", "duedate");
  
  
                  //Entity contact = service.Retrieve("contact", cid, cols);
                  //contact.Attributes["jobtitle"] = "Sometitle";
                  // contact.Attributes["address1_line1"] = "Some address";
                  //service.Update(contact);
  
  
                  string fetchCommissionQuery = @"
  &lt;fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'&gt;
    &lt;entity name='tbw_commission'&gt;
      &lt;attribute name='tbw_commissionid' /&gt;
      &lt;attribute name='tbw_name' /&gt;
      &lt;attribute name='createdon' /&gt;
      &lt;order attribute='tbw_name' descending='false' /&gt;
      &lt;filter type='and'&gt;
        &lt;condition attribute='new_relatedinvoice' operator='eq' uitype='invoice' value='";
  
                  fetchCommissionQuery += invoiceID;
                  fetchCommissionQuery += @"' /&gt;
      &lt;/filter&gt;
    &lt;/entity&gt;
  &lt;/fetch&gt; ";
  
  
                  EntityCollection commissionCollection = service.RetrieveMultiple(new FetchExpression(fetchCommissionQuery));
  
                  //targetInvoice = service.Retrieve("invoice", invoiceID, invoiceCols);
  
                  #endregion
  
                  foreach (var commission in commissionCollection.Entities)
                  {
                      if (targetInvoice.Attributes["duedate"] != null)
                      {
  
                          commission.Attributes["tbw_duedate"] = targetInvoice.Attributes["duedate"];
                      }
  
                      if (targetInvoice.Attributes["new_invoiceamount"] != null)// &amp;&amp; commission.Attributes["tbw_paymentrate"] != null)
                      {
                          //dynamic commRate = commission.Attributes["tbw_paymentrate"];
  
                          //dynamic invoiceTotal = ((Money)targetInvoice.Attributes["new_invoiceamount"]).Value;
  
                          //dynamic commTotal = commRate * invoiceTotal;
  
                          //commission.Attributes["tbw_total"] = new Money(commTotal);
                          //commission.Attributes["tbw_total"] = 1.02;
                      }
  
                      //commission.Attributes["tbw_name"] = "TestCommName";
                      service.Update(commission);
  
                  }
  
  
                  // dynamic currentInvoiceTotal = ((Money)targetInvoice.Attributes["new_invoiceamount"]).Value;
  
                  // currentInvoiceTotal = currentInvoiceTotal - 10;
  
                  // Entity invoice = service.Retrieve("invoice", invoiceID, invoiceCols);
                  // invoice.Attributes["new_grossprofit"] = new Money(currentInvoiceTotal);
                  //service.Update(invoice);
  
                  //}
  
              }
          }
      }
  }
  </code></pre>