Skip to main content

Notifications

Community site session details

Community site session details

Session Id : qqYx9bSFUmNwTZaqyoRlXW
Power Apps - Building Power Apps
Unanswered

C# Plugin error "The given key was not present in the dictionary"

Like (0) ShareShare
ReportReport
Posted on 12 Jun 2023 06:33:15 by 98

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.

Categories:
  • Salim_A_M Profile Picture
    98 on 12 Jun 2023 at 10:11:41
    Re: C# Plugin error "The given key was not present in the dictionary"

    Unfortunately that still doesn't solve it.

    Thank you for your time, I'll have to look into something else.


    Regards.

  • VIDIT Profile Picture
    666 on 12 Jun 2023 at 09:25:21
    Re: C# Plugin error "The given key was not present in the dictionary"

    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

  • Salim_A_M Profile Picture
    98 on 12 Jun 2023 at 09:00:33
    Re: C# Plugin error "The given key was not present in the dictionary"

    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.

  • VIDIT Profile Picture
    666 on 12 Jun 2023 at 08:42:19
    Re: C# Plugin error "The given key was not present in the dictionary"

    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]

  • Salim_A_M Profile Picture
    98 on 12 Jun 2023 at 08:37:48
    Re: C# Plugin error "The given key was not present in the dictionary"

    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.

  • VIDIT Profile Picture
    666 on 12 Jun 2023 at 08:35:50
    Re: C# Plugin error "The given key was not present in the dictionary"

    Hi @Salim_A_M are you sure Column 10 is present in the environment in which this plugin is deployed ?

  • Salim_A_M Profile Picture
    98 on 12 Jun 2023 at 08:32:40
    Re: C# Plugin error "The given key was not present in the dictionary"

    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.

  • VIDIT Profile Picture
    666 on 12 Jun 2023 at 07:47:23
    Re: C# Plugin error "The given key was not present in the dictionary"

    Hi @Salim_A_M  

     

    You are putting the attribute name incorrectly or the entity name incorrectly.

  • VIDIT Profile Picture
    666 on 12 Jun 2023 at 07:47:21
    Re: C# Plugin error "The given key was not present in the dictionary"

    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.

    • Columns logical names are correctly entered.
    • Entity name is correctly entered.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,668 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,004 Most Valuable Professional

Leaderboard