title: The given key was not present in the dictionary Plugin CRM 2011 online
body: <p>Could anyone tell me what I am doing wrong, I've been trying for over a week now.</p>
  
  <p>Follow the code.</p>
  
  <blockquote>
    <p>Unexpected exception from plug-in (Execute):
    Microsoft.Crm.Sdk.Samples.ProjectTotalAmount:
    System.Collections.Generic.KeyNotFoundException: The given key was not
    present in the dictionary.</p>
  </blockquote>
  
  <pre class="lang-cs prettyprint-override"><code>namespace Microsoft.Crm.Sdk.Samples
  {
      public class ProjectTotalAmount : IPlugin
      {
  
          public void Execute(IServiceProvider serviceProvider)
          {
  
              Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
  
              if (context.InputParameters.Contains("Target") &amp;&amp;
                  context.InputParameters["Target"] is Entity)
              {
  
                  IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                  IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
  
                  //create a service context
                  var ServiceContext = new OrganizationServiceContext(service);
                  //ITracingService tracingService = localContext.TracingService;
  
                  Entity entity = (Entity)context.InputParameters["Target"];
  
  
                  if (entity.LogicalName == "new_project")
                  {
  
  
                      Guid projectGUID = ((EntityReference)entity["new_project"]).Id;
                      Entity a = service.Retrieve("new_project", ((EntityReference)entity["new_project"]).Id, new ColumnSet(true));
  
                      decimal totalAmount = 0;
  
                      try
                      {
                          //fetchxml to get the sum total of estimatedvalue
                          string new_amount_sum = string.Format(@" 
                          &lt;fetch distinct='false' mapping='logical' aggregate='true'&gt; 
                              &lt;entity name='new_projectitem'&gt; 
                                  &lt;attribute name='new_amount' alias='new_amount' aggregate='sum' /&gt; 
                                  &lt;filter type='and'&gt;
                                      &lt;condition attribute='new_projectid' operator='eq' value='{0}' uiname='' uitype='' /&gt;
                                  &lt;/filter&gt;
                              &lt;/entity&gt;
                          &lt;/fetch&gt;", a.Id);
  
                          EntityCollection new_amount_sum_result = service.RetrieveMultiple(new FetchExpression(new_amount_sum));
  
                          foreach (var c in new_amount_sum_result.Entities)
                          {
                              totalAmount = ((Money)((AliasedValue)c["new_amount_sum"]).Value).Value;
                          }
  
                          //updating the field on the account
                          Entity acc = new Entity("new_project");
                          acc.Id = a.Id;
                          acc.Attributes.Add("new_amount", new Money(totalAmount));
                          service.Update(acc);
  
  
                      }
                      catch (FaultException ex)
                      {
                          throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                      }
                  }
              }
  
          }
      }   
  }
  </code></pre>
  
  <p>The settings for the plugin:</p>
  
  <p>Post-validation
  Synchronous execution mode
  Server deployment</p>