web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Plugin - The given key was not present in the dictionary

(0) ShareShare
ReportReport
Posted on by 1,799

Hi, 

I'm trying to create a plugin that will update the annual revenue of a parent Account of a Contact with a static value defined in a plugin. 

This is just a throwaway plugin to test behavior of a scenario we are running (the scenario is a little nonsensical but there is reasoning behind it). The plugin fires on update of the email field on the Contact.

As a non-developer I've used the Microsoft documentation and a couple of blog posts to come up with the following code.

I've tried an number of different versions of the code but always get the error "The given key was not present in the dictionary". From what I understand, this would suggest the Target or Attributes I'm accessing are not present in the dictionary. I can't understand why that would be the case. If it's relevant, the Annual Revenue field currently contains data on the Parent Account. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;

namespace CalculatedFieldPlugin
{
    public class CalculationTest : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {

            // Obtain the tracing service
            ITracingService tracingService =
            (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.  
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            // The InputParameters collection contains all the data passed in the message request.  
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.  
                Entity entity = (Entity)context.InputParameters["Target"];
                EntityReference a = (EntityReference)entity.Attributes["parentcustomerid"];

                // Obtain the organization service reference which you will need for  
                // web service calls.  
                IOrganizationServiceFactory serviceFactory =
                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                try
                {

                        // Plug-in business logic goes here. 
                        //updating the field on the account
                        Entity acc = new Entity("account");
                        acc.Id = a.Id;
                        acc.Attributes.Add("revenue", new Money(10));
                        service.Update(acc);
 
                }

                catch (FaultException ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
                }

                catch (Exception ex)
                {
                    tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
                    throw;
                }
            }

        }

    }
}

*This post is locked for comments

I have the same question (0)
  • JDMCRM Profile Picture
    1,799 on at
    RE: Plugin - The given key was not present in the dictionary

    Hi Arun, you're absolutely right. Made the change and it now works without issue. Thank you for the help.

  • Verified answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at
    RE: Plugin - The given key was not present in the dictionary

    In the below line of code:

    EntityReference a = (EntityReference)entity.Attributes["parentcustomerid"];

    You said this plugin triggers on update of email. If the parentcustomerid field is not updated, it wont be in "target" entity. You have to grab from "preImage". Thats why high possibility of error coming in below line bcoz "a" is null.

    acc.Id = a.Id;

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans