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.
Unfortunately that still doesn't solve it.
Thank you for your time, I'll have to look into something else.
Regards.
Oh Now I understand properly,
Can you try doing
if (a1 != null )
{
if(a1.Contains("ccc_valueName"))
{
...........
}
}
I don't see other problem with the code
Here's the process that I coded as I imagine it in my head:
I fetch a1, of which there can only ever be 1 record, if any.
If it exists, then FirstOrDefault() returns that record, of which I extract "ccc_valueName" and assign it to the new record. If it doesn't, then FirstOrDefault() returns null, which is where the if statement comes in: in case the fetched record doesn't exist, then I simply skip adding it to the record to be created.
I understand your reasoning regarding the potential error regarding the names, but the error just can't possibly be that because when the fetched record exists, the plugin runs correctly and creates the record with the lookup fetched through a1 being correctly set.
The error happens if the fetched record doesn't exist, so when the line { recordToBeCreated["column10"] = a["ccc_valueName"]; } doesn't run at all. It's as if the "column10" is required for the record to be created in the first place, but I don't know if that's possible.
Also try if you are getting "ccc_valueName" this column from the fetch well I think you must be getting it else the error would have being object reference not set to instance of an object.
also can you try doing a1.getattributes<attributetype>[ccc_valueName] instead of a1[ccc_valueName]
Hello, yes I am, it's even available in the entity's form, from which I copied the logical name to be sure there is no mistake.
Hi @Salim_A_M are you sure Column 10 is present in the environment in which this plugin is deployed ?
Hello, thank you for your reply.
I have reviewed the plugin and can assure you all the logical names are entered correctly.
The error is actually happening if I omit the "column10" mentioned in the post, which I found counter-intuitive.
In other words, if this line executes: recordToBeCreated["column10"] = a["ccc_valueName"];
everything is fine. If the fetched entity is null and it doesn't, then I get the error, which is what confused me.
Hi @Salim_A_M
Given key is not present means you plugin is unable to get the context of the key values you are passing. please check the below.
WarrenBelz
223
Most Valuable Professional
MS.Ragavendar
110
stampcoin
80