Take the 2-minute tour ×

Could anyone tell me what I am doing wrong, I've been trying for over a week now.

Follow the code.

Unexpected exception from plug-in (Execute): Microsoft.Crm.Sdk.Samples.ProjectTotalAmount: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

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") &&
                context.InputParameters["Target"] is Entity)

                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                //create a service context
                var ServiceContext = new OrganizationServiceContext(service);
                //ITracingService tracingService = localContext.TracingService;


                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;

                        //fetchxml to get the sum total of estimatedvalue
                        string new_amount_sum = string.Format(@" 
                            <entity name='new_projectitem'> 
                                <attribute name='new_amount' alias='new_amount' aggregate='sum' /> 
                                <filter type='and'>
                                    <condition attribute='new_projectid' operator='eq' value='{0}' uiname='' uitype='' />
                        </fetch>", 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));

                    catch (FaultException ex)


The settings for the plugin:

Post-validation Synchronous execution mode Server deployment

share|improve this question

1 Answer 1

up vote 3 down vote accepted

A few pointers to help you before we start looking at your code...

  • This error usually means your code is referring to an attribute that does not exist (or does not have a value)
  • You haven't said on which message your plugin is registered. This may affect the available parameters at runtime
  • You've commented out your tracingService variable but this can help you at least see how far your code has come. Reinstate it and add a few lines such as this to track your progress prior to the failure. This information will be written to the error log that is offered to you in the client side Exception dialog.:

    tracingService.Trace("Project Id is {0}", projectGUID);` 


    tracingService.Trace("Number of returned records: {0}", new_amount_sum_result.Entities.Count);`
  • The following line seems entirely redundant since you are only using the attribute Id from a and this already exists as entity.Id:


share|improve this answer

Your Answer


