0
votes

I have written a C# plugin for post update of the parent record based on the multiple fields. In this I am trying to calculate the total value in the parent entity based on the values updated in the child entity, which has rate and units fields in it. So basically, total=rate*unit. The code builds fine, but when creating a new form in dynamics crm it genetrates a Business Process Error: Unexpected exception from plug-in (Execute): Parentchild1.parentchildpluginSystem.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

Here is my code:

namespace Parentchild1 { public class parentchildplugin : IPlugin { private Entity abcevent_parent;

    public void Execute(IServiceProvider serviceProvider)
    {

        // Obtain the execution context from the service provider.
        IPluginExecutionContext context =
            (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

        // Get a reference to the Organization service.
        IOrganizationServiceFactory factory =
            (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = factory.CreateOrganizationService(context.UserId);

        if (context.InputParameters != null)
        {
            //entity = (Entity)context.InputParameters["Target"];
            //Instead of getting entity from Target, we use the Image
            Entity entity = context.PostEntityImages["PostImage"];

            Money rate = (Money)entity.Attributes["abcevent_rate"];
            int unit = (int)entity.Attributes["abcevent_unit"];
           // EntityReference parent = (EntityReference)entity.Attributes["abcevent_parentid"];

            //Multiply
           // Money total = new Money(rate.Value * units);



            //Set the update entity
            Entity parententity = new Entity("abcevent_parent");
            //parententity.Id = parent.Id;
            //parententity.Attributes["abcevent_total"] = total;

           // abcevent_parentid = Guid IOrganizationservice.Create(Entity parentid);

            Guid parentGuid = service.Create(abcevent_parent);

            EntityReference parent = (EntityReference)entity.Attributes["abcevent_parentid"];

            Money total = new Money(rate.Value * unit);



            //Update
            //service.Update(parententity);

        }
1

1 Answers

0
votes

the problem you ask for:

The given key was not present in the dictionary.

It is because one key is not in the list, if you try to get an attribute like

int number = (int)entity.Attributes["random_attribute"]

This throws the error, because random_attribute it is not in the context.

You have to make sure the attribute is in the context... the best practice to this is asking for a Contains:

if (entity.Contains("random_attribute"))

This way you know you can safely access to the attribute.

Another reason may be the Image, make sure it is in the context.