Asked by:
The given key was not present in the dictionary

Question
-
Hi guys,
I am developing a Plugin for CRM 2011 and I am getting The given key was not present in the dictionary on update. The create works fine. My plugin sums a field on child records to another field on the parent.
Parent Entity (v1_simontest), field containing summed child values (v1_totalfundingallocated)
Child Entity (v1_simontestsub) field that is being summed (v1_amountallocated)
See my code below. I have a create and an update step registered, both on Post Operation. Would really appreciate any help on this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;
namespace SimonTestDttas
{
public class SimonTest : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{// Get the IPlugin Context
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));//Get a IOrganizationService
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];//get the customerid
EntityReference a = (EntityReference)entity.Attributes["v1_simontestparent"];decimal totalAmount = 0;
try
{
//fetchxml to get the sum total of estimatedvalue
string amountallocated_sum = string.Format(@"
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='v1_simontestsub'>
<attribute name='v1_amountallocated' alias='amountallocated_sum' aggregate='sum' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0' />
<condition attribute='v1_simontestparent' operator='eq' value='{0}' uiname='' uitype='' />
</filter></entity>
</fetch>", a.Id);
EntityCollection amountallocated_sum_result = service.RetrieveMultiple(new FetchExpression(amountallocated_sum));foreach (var c in amountallocated_sum_result.Entities)
{
totalAmount = ((Money)((AliasedValue)c["amountallocated_sum"]).Value).Value;
}//updating the field on the account
Entity acc = new Entity("v1_simontest");
acc.Id = a.Id;
acc.Attributes.Add("v1_totalfundingallocated", new Money(totalAmount));
service.Update(acc);
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}}
}
}
Wednesday, May 28, 2014 8:55 AM
All replies
-
Hi guys,
I am developing a Plugin for CRM 2011 and I am getting The given key was not present in the dictionary on update. The create works fine. My plugin sums a field on child records to another field on the parent.
Parent Entity (v1_simontest), field containing summed child values (v1_totalfundingallocated)
Child Entity (v1_simontestsub) field that is being summed (v1_amountallocated)
See my code below. I have a create and an update step registered, both on Post Operation. Would really appreciate any help on this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;
namespace SimonTestDttas
{
public class SimonTest : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{// Get the IPlugin Context
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));//Get a IOrganizationService
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];//get the customerid
EntityReference a = (EntityReference)entity.Attributes["v1_simontestparent"];decimal totalAmount = 0;
try
{
//fetchxml to get the sum total of estimatedvalue
string amountallocated_sum = string.Format(@"
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='v1_simontestsub'>
<attribute name='v1_amountallocated' alias='amountallocated_sum' aggregate='sum' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0' />
<condition attribute='v1_simontestparent' operator='eq' value='{0}' uiname='' uitype='' />
</filter></entity>
</fetch>", a.Id);
EntityCollection amountallocated_sum_result = service.RetrieveMultiple(new FetchExpression(amountallocated_sum));foreach (var c in amountallocated_sum_result.Entities)
{
totalAmount = ((Money)((AliasedValue)c["amountallocated_sum"]).Value).Value;
}//updating the field on the account
Entity acc = new Entity("v1_simontest");
acc.Id = a.Id;
acc.Attributes.Add("v1_totalfundingallocated", new Money(totalAmount));
service.Update(acc);
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}}
}
}
- Merged by Andrii ButenkoMVP, Moderator Wednesday, May 28, 2014 9:13 AM The same thread
Wednesday, May 28, 2014 8:56 AM -
Hi guys,
I am developing a Plugin for CRM 2011 and I am getting The given key was not present in the dictionary on update. The create works fine. My plugin sums a field on child records to another field on the parent.
Parent Entity (v1_simontest), field containing summed child values (v1_totalfundingallocated)
Child Entity (v1_simontestsub) field that is being summed (v1_amountallocated)
See my code below. I have a create and an update step registered, both on Post Operation. Would really appreciate any help on this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;
namespace SimonTestDttas
{
public class SimonTest : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{// Get the IPlugin Context
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));//Get a IOrganizationService
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];//get the customerid
EntityReference a = (EntityReference)entity.Attributes["v1_simontestparent"];decimal totalAmount = 0;
try
{
//fetchxml to get the sum total of estimatedvalue
string amountallocated_sum = string.Format(@"
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='v1_simontestsub'>
<attribute name='v1_amountallocated' alias='amountallocated_sum' aggregate='sum' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0' />
<condition attribute='v1_simontestparent' operator='eq' value='{0}' uiname='' uitype='' />
</filter></entity>
</fetch>", a.Id);
EntityCollection amountallocated_sum_result = service.RetrieveMultiple(new FetchExpression(amountallocated_sum));foreach (var c in amountallocated_sum_result.Entities)
{
totalAmount = ((Money)((AliasedValue)c["amountallocated_sum"]).Value).Value;
}//updating the field on the account
Entity acc = new Entity("v1_simontest");
acc.Id = a.Id;
acc.Attributes.Add("v1_totalfundingallocated", new Money(totalAmount));
service.Update(acc);
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}}
}
}
- Merged by Andrii ButenkoMVP, Moderator Wednesday, May 28, 2014 9:11 AM The same thread
Wednesday, May 28, 2014 8:57 AM -
Hi guys,
I am developing a Plugin for CRM 2011 and I am getting The given key was not present in the dictionary on update. The create works fine. My plugin sums a field on child records to another field on the parent.
Parent Entity (v1_simontest), field containing summed child values (v1_totalfundingallocated)
Child Entity (v1_simontestsub) field that is being summed (v1_amountallocated)
See my code below. I have a create and an update step registered, both on Post Operation. Would really appreciate any help on this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;
namespace SimonTestDttas
{
public class SimonTest : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Get the IPlugin Context
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
//Get a IOrganizationService
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];
//get the customerid
EntityReference a = (EntityReference)entity.Attributes["v1_simontestparent"];
decimal totalAmount = 0;
try
{
//fetchxml to get the sum total of estimatedvalue
string amountallocated_sum = string.Format(@"
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='v1_simontestsub'>
<attribute name='v1_amountallocated' alias='amountallocated_sum' aggregate='sum' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0' />
<condition attribute='v1_simontestparent' operator='eq' value='{0}' uiname='' uitype='' />
</filter></entity>
</fetch>", a.Id);
EntityCollection amountallocated_sum_result = service.RetrieveMultiple(new FetchExpression(amountallocated_sum));
foreach (var c in amountallocated_sum_result.Entities)
{
totalAmount = ((Money)((AliasedValue)c["amountallocated_sum"]).Value).Value;
}
//updating the field on the account
Entity acc = new Entity("v1_simontest");
acc.Id = a.Id;
acc.Attributes.Add("v1_totalfundingallocated", new Money(totalAmount));
service.Update(acc);
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
}
}
}- Merged by Andrii ButenkoMVP, Moderator Wednesday, May 28, 2014 9:08 AM The same thread
Wednesday, May 28, 2014 8:58 AM -
See http://mscrmuk.blogspot.co.uk/2013/04/the-given-key-was-not-present-in.html - I expect you've not got a value in the v1_simontestparent attribute as it's not being changed by the update
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
Wednesday, May 28, 2014 10:46 AMModerator -
Hi, you register your plugin in PostUpdate right?
Then, I assume your current update is when the value of the v1_amountallocated field is changed, for example from 100 to 1000.
Actually, if only that field that changed and your parent, which is :
is still remain the same value, this
entity.Attributes["v1_simontestparent"]
will be null, since the entity is :
Entity entity = (Entity)context.InputParameters["Target"];
not the postImageEntity or not the entity that having this attribute value ["v1_simontestparent"], because you don't update this ["v1_simontestparent"] value, so that this value will never be the context.InputParameters, so that you found the "Thie given key was not present in the dictionary" because in the entity context input parameter, currently you only have the value that has changing, not all of the field value inside your entity.
You have retrieve your entity, can be through //get the current entity
Entity currEntity = new Entity("v1_simontestsub");
currEntity = service.Retrieve("v1_simontestsub", entity.Id, new ColumnSet(true));or you can use post image
Change to this :
//get the current entity Id
Entity currEntity = new Entity("v1_simontestsub");
currEntity = service.Retrieve("v1_simontestsub", entity.Id, new ColumnSet(true));
//get the customerid
//change thisEntityReference a = (EntityReference)entity.Attributes["v1_simontestparent"];
//to this:
if (currEntity.Attributes.Contains("v1_simontestparent"))
{
EntityReference a = (EntityReference)currEntity.Attributes["v1_simontestparent"];
}Hope it helps!
Wednesday, May 28, 2014 5:51 PM -
You don't seem to have a contains check before line
totalAmount = ((Money)((AliasedValue)c["amountallocated_sum"]).Value).Value;
You should first check if the entity contains that attribute. This is because, if there is no value in the attribute, the attribute won't be returned by CRM.
Wednesday, May 28, 2014 6:34 PM