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.