user1439574
user1439574

Reputation: 33

The given key was not present in the dictionary Plugin CRM 2011 online

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)
            {

                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(@" 
                        <fetch distinct='false' mapping='logical' aggregate='true'> 
                            <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='' />
                                </filter>
                            </entity>
                        </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));
                        service.Update(acc);


                    }
                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                    }
                }
            }

        }
    }   
}

The settings for the plugin:

Post-validation Synchronous execution mode Server deployment

Upvotes: 1

Views: 10914

Answers (1)

Greg Owens
Greg Owens

Reputation: 3878

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);` 
    

and

    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:

    Entity a = service.Retrieve("new_project", ((EntityReference)entity["new_project"]).Id, new ColumnSet(true));

Upvotes: 3

Related Questions