2
votes

I am trying to create a new product with a plugin, but I get this exception:

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

This is the code to create the product

EntityReference ugRef = new EntityReference(ug.LogicalName, ug.UoMScheduleId.Value);
EntityReference uRef = new EntityReference(u.LogicalName, u.UoMId.Value);   
Product product = new Product()
{
  Name = pName,
  ProductNumber = pNumber,
  QuantityDecimal = 2,
  DefaultUoMScheduleId = ugRef,
  DefaultUoMId = uRef
}; 
service.Create(product);

All variables have been tested, they all have values. The unit is correct for the unit group - if I change either I get an exception saying as much.

The problem is definitely with this piece of the code as there is a lovely lead with the expected 1st and last name when the code is altered to this:

EntityReference ugRef = new EntityReference(ug.LogicalName, ug.UoMScheduleId.Value);
EntityReference uRef = new EntityReference(u.LogicalName, u.UoMId.Value);
Lead l = new Lead();
l.FirstName = uRef.Id.ToString();
l.LastName = uRef.LogicalName;
service.Create(l);
/*
  Product product = new Product()
  {
    Name = (String)staged.Attributes["wishlist_name"],
    ProductNumber = (String)staged.Attributes["wishlist_barcode"],
    QuantityDecimal = 2,
    DefaultUoMScheduleId = ugRef,
    DefaultUoMId = uRef
  }; 
  service.Create(product);
*/

pName and pNumber are strings.
u and ug are a Unit and a Unit Group.

I changed the code to:

            query = new QueryByAttribute("uom");
            query.ColumnSet = new ColumnSet("name", "uomscheduleid");
            query.Attributes.AddRange("name");
            query.Values.AddRange("1");
            UoM unit = (UoM)service.RetrieveMultiple(query).Entities[0];                    
            Product newProduct = new Product
            {

                ProductNumber = "1t2y3u",
                Name = "Example Banana Product",
                QuantityDecimal = 1,
                DefaultUoMScheduleId = unit.UoMScheduleId,
                DefaultUoMId = unit.ToEntityReference()
            };
            service.Create(newProduct);

The same error is thrown.

I am about to strip my moer with this.

1

1 Answers

2
votes

Couple things to look at.

  1. It looks like your tried to simplify your code in the first example, but may have removed the source of your bug, but luckily, you added it to your last example :) I'm guessing staged does not contain "wishlist_name", and therefor is giving you the error you see. You should always use the typed GetAttributeValue method defined in the Entity class: staged.GetAttributeValue<String>("wishlist_name"). It will perform a null check and return the default for the type.

  2. Check all of your other plugins, to see if another plugin is fired upon the creation of the Product, that is possibly doing some extra logic if the DefaultUoMScheduleId or DefaultUoMId is populated. Your create in this plugin could be getting an error from another "nested" plugin.

  3. Instead of creating temporary Entity Reference variables, use the ToEntityReference() method defined in the entity class, it makes the code look a little cleaner IMHO.

        Product product = new Product()
        {
            Name = (String)staged.Attributes["wishlist_name"],
            ProductNumber = (String)staged.Attributes["wishlist_barcode"],
            QuantityDecimal = 2,
            DefaultUoMScheduleId = ug.ToEntityReference(),
            DefaultUoMId = u.ToEntityReference()
    
        };