Hello, I am facing a familiar error "The given key was not present in the dictionary" while developing a C# plugin, in a new situation. Here is the scenario:
.... some previous code ....
Entity a1 = service.RetrieveMultiple(new FetchExpression( "fetch here" )).Entities.FirstOrDefault();
Entity recordToBeCreated = new Entity("Entity name");
recordToBeCreated["column1"] = ... value1 ...;
recordToBeCreated["column2"] = ... value2 ...;
... fill all values, finally:
if (a1 != null && a1.Contains("ccc_valueName")) (ccc_valueName being a placeholder for some logical name)
recordToBeCreated["column10"] = a["ccc_valueName"];
service.Create(recordToBeCreated);
Here, if the if statement returns false and "ccc_valueName" doesn't exist or a1 is null, the creation of the records throws an error.
I have no Idea why this happens, when the expected behavior is that the value of "ccc_valueName" should simply not be included in the created record. The record "column10" in the record to be created IS required, however in other instances, this hasn't stopped the plugin from running, creating a record with the required value simply being empty.
Debugging with the profiler, the exact line throwing the error is the service.Create(...) line;
I have no idea what to do, so any help would be greatly appreciated.