Answered by:
Unexpected exception from plug-in (Execute): FeePaidUpdatePayment.Plugins.PostFeepaidUpdate: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

Question
-
Hello,
I am kind of new in MS CRM 2011. I am going a plug-in that is taking some fields from grid view sum them and returning the sum in Target Entity. On post-event plug-in, following error occurs. Any help is welcome. Thank you in advance.
Unexpected exception from plug-in (Execute): FeePaidUpdatePayment.Plugins.PostFeepaidUpdate: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
CODE:
namespace FeePaidAssociate.AssociatePlugin { using System; using System.ServiceModel; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; public class PostFeepaidCreate: Plugin { public PostFeepaidCreate() : base(typeof(PostFeepaidCreate)) { base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Associate", "", new Action<LocalPluginContext>(ExecutePostFeepaidCreate))); } protected void ExecutePostFeepaidCreate(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService tracingService = localContext.TracingService; try { if (!context.InputParameters.Contains("Target")) { return; } if (context.InputParameters["Target"] is EntityReference) { if (context.MessageName == "Associate") { EntityReference targetEntity = null; string relationshipName = string.Empty; EntityReferenceCollection relatedEntities = null; EntityReference relatedEntity = null; if (!context.InputParameters.Contains("Target")) { return; } EntityReference paid = (EntityReference)context.InputParameters["Target"]; Entity paidE = new Entity("integral_feespaid"); Guid feePaidID = (Guid)((EntityReference)context.InputParameters["Target"]).Id; if (paid.LogicalName != "integral_feespaid") { return; } Relationship relationship = (Relationship)context.InputParameters["Relationship"]; if (relationship.SchemaName != "zbg_integral_feespaid_zbg_fee_payment") { return; } // Get Related Entities EntityReferenceCollection re = (EntityReferenceCollection)context.InputParameters["RelatedEntities"]; foreach (EntityReference rel in re) { Entity relatedEntityFee = service.Retrieve("zbg_fee_payment", rel.Id, new ColumnSet("zbg_totalamount")); decimal totalA = (Decimal)relatedEntityFee["zbg_totalamount"]; System.Diagnostics.Debug.Write(totalA); setVal(paidE, "integral_feespaidid", feePaidID); setVal(paidE, "zbg_payments_totalamount", totalA); paidE["zbg_payments_totalamount"] = totalA; } //Here I am using second method to take the sum from the grid records because i still don't know witch one is better //Decimal Total = FetchResult(feePaidID, service); // Entity updFeePaid = new Entity("integral_feespaid"); //paidE.Id = feePaidID; // setVal(paidE, "zbg_payments_totalamount", Total); service.Update(paidE); } } } catch (FaultException<OrganizationServiceFault> ex) { throw; } } public void setVal(Entity entity, string attr, object val) { if (entity.Attributes.Contains(attr)) { entity[attr] = val; } else { entity.Attributes.Add(attr, val); } } private static decimal FetchResult(Guid feePaidID, IOrganizationService service) { string fetchSums = @" <fetch distinct='false' mapping='logical' aggregate='true'> <entity name='zbg_fee_payment'> <attribute name='zbg_totalamount' alias='zbg_totalamount_sum' aggregate='sum' /> <link-entity name='zbg_integral_feespaid_zbg_fee_payment' alias='aa' from='zbg_fee_paymentid' to='zbg_fee_paymentid'> <filter type='and'> <condition attribute='integral_feespaidid' operator='eq' value='" + feePaidID + @"'/> </filter> </link-entity> </entity> </fetch>"; decimal TotalAmount = 0; fetchSums = string.Format(fetchSums, feePaidID); var tupoValue = new FetchExpression(fetchSums);//zapisva vseki rezultat v collection EntityCollection result = service.RetrieveMultiple(tupoValue); // EntityCollection result = (EntityCollection)service.RetrieveMultiple(new FetchExpression(fetchSums)); Entity updPayment = new Entity("zbg_fee_payment"); foreach (var pay in result.Entities) { decimal aggregate = (Decimal)((AliasedValue)pay["zbg_totalamount_sum"]).Value; System.Diagnostics.Debug.Write(aggregate); TotalAmount = aggregate; } return TotalAmount; } } }
CODE from RegisterFile.crmregister:
<?xml version="1.0" encoding="utf-8"?> <Register xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/crm/2011/tools/pluginregistration"> <Solutions> <Solution Assembly="FeePaidAssociate.AssociatePlugin.dll" Id="c081ea61-954b-e311-adbb-00155df2b603" IsolationMode="None" SourceType="Database"> <PluginTypes> <Plugin Description="Plug-in to PostFeepaidCreate" FriendlyName="PostFeepaidCreate" Name="FeePaidAssociate.AssociatePlugin.PostFeepaidCreate" Id="fb1bf883-954b-e311-adbb-00155df2b603" TypeName="FeePaidAssociate.AssociatePlugin.PostFeepaidCreate"> <Steps> <clear /> <Step CustomConfiguration="" Name="PostFeepaidCreate" Description="Post-Operation of Fee paid Create" Id="fc1bf883-954b-e311-adbb-00155df2b603" MessageName="Associate" Mode="Synchronous" PrimaryEntityName="" Rank="1" SecureConfiguration="" Stage="PostOutsideTransaction" SupportedDeployment="ServerOnly"> <Images /> </Step> </Steps> </Plugin> </PluginTypes> </Solution> </Solutions> <XamlWorkflows /> </Register>
Wednesday, November 13, 2013 9:45 AM
Answers
-
Hi,
First, I would check if all of the attributes you are referencing do exist. You are using "Relationship", "RelatedEntities" from input parameters and "zbg_totalamount" from the zbg_free_payment entity before checking if they exist. Most probably you are missing one of those, that is why you get a key not found exception.
Regards,
Adam
- Marked as answer by Tsankova Monday, June 1, 2015 8:24 AM
Thursday, November 14, 2013 10:31 AM