Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

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

(0) ShareShare
ReportReport
Posted on by 25

Hi,

A Newbie here! I am trying my hands on Dynamics programming and testing out a basic code to update a custom entity that I created in a plugin.

Task I am trying to code is: when "sales rep" is assgined to to a zone (custom field "crdc2_repinzoneid") set field "status" (type:two option, name:crdc2_status) to "assigned"  (See screenshot below)

Step is registered to fire on Entity Update at PostOperation (see screenshot below).

This is what I did in plugin code:

1. Create a new entity "sales rep" using context.PrimaryEntityId

Entity salesRep = new Entity("crdc2_salesrep", context.PrimaryEntityId);

2. Create attribute which needs to be updated:

salesRep["crdc2_status" = entity["crdc2_repinzoneid" == null ? false : true; //set value

3. Fire Update

service.Update(salesRep);

I debugged the plugin and at #3 I get the exception  System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary

I fail to understand the reason here. The name "crdc2_status" exists in "crdc2_salesrep" entity.

What am I missing here!

Code:

public void Execute(IServiceProvider serviceProvider)
    {
      //Extract the tracing service for use in debugging sandboxed plug-ins.
      ITracingService tracingService =
          (ITracingService)serviceProvider.GetService(typeof(ITracingService));
      IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

      tracingService.Trace("SalesRepAssignedPlugin plgin: start execution4");
      if (context.InputParameters.Contains("Target") &&
          context.InputParameters["Target"] is Entity)
      {
        try
        {
          Entity entity = (Entity)context.InputParameters["Target"];
          var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
          var service = serviceFactory.CreateOrganizationService(context.UserId);

          if (entity.LogicalName != "crdc2_salesrep")
            return;

          //create new entity with only fields that needs to be updated
          Entity salesRep = new Entity("crdc2_salesrep", context.PrimaryEntityId);
          //create attrib to update
          salesRep["crdc2_status"] = entity["crdc2_repinzoneid"] == null ? false : true;            //set value

          service.Update(salesRep);
        }
        catch (FaultException ex)
        {
          tracingService.Trace("SalesRepAssignedPlugin plgin ex: {0}", ex.ToString());
          throw new InvalidPluginExecutionException("SalesRepAssignedPlugin plgin:siyapa!", ex);
        }
        catch (Exception ex)
        {
          tracingService.Trace("SalesRepAssignedPlugin plgin ex: {0}", ex.ToString());
          throw;
        }

      }

Custom Entity Fields

sales-rep-fields.jpg

Plugin step:

step.jpg

Thanks,

Jags

  • LuHao Profile Picture
    40,888 on at
    RE: Custom Entity update: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary

    Glad it was resolved.

  • Verified answer
    Jags8 Profile Picture
    25 on at
    RE: Custom Entity update: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary

    I was finally able to resolve this!

    1. I ended up using PostImage to pick the field value being updated:

    2. Another thing which I learned is that when a lookup field is set to blank then the attribute value, instead of being NULL, is not included in image at all!

    so this check throws an exception when lookup field is not set:

    postImageRep["crdc2_repinzoneid"] == null

    correct code to check for blank/empty lookup field is 

    postImageRep.Attributes.Contains("crdc2_repinzoneid") == false;

    Thanks!

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

News and Announcements

Now Available: 2025 Release Wave 2

Quick Links

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Adis Profile Picture

Adis 136 Super User 2025 Season 1

#2
Sohail Ahmed Profile Picture

Sohail Ahmed 81

#3
Jonas "Jones" Melgaard Profile Picture

Jonas "Jones" Melgaard 77 Super User 2025 Season 1

Product updates

Dynamics 365 release plans