Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

The given key was not present in the dictionary.

Posted on by 35

public class InterviewersPlugin : IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {

            IPluginExecutionContext context =

                (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory factory =

                (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationService service =

                factory.CreateOrganizationService(context.UserId);

             

             

var fetchXml = $@"<?xml version=""1.0"" encoding=""utf-16""?>

                                <fetch>

                                <entity name=""cr884_recruitmentprojectinterviewers"">

                                <attribute name=""cr884_interviewnumber"" />

                                <attribute name=""cr884_recruitmentproject"" />

                                </entity>

                                </fetch>";

            EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchXml));

            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

            {

                var x = 0;

                Entity entity = (Entity)context.InputParameters["Target"];

                Entity ret = service.Retrieve("cr884_recruitmentprojectinterviewers", entity.Id, new ColumnSet(true));

                Guid id = ((EntityReference)ret.Attributes["cr884_recruitmentproject"]).Id;

                foreach (Entity e in result.Entities)

                {

                    if (!id.Equals(((EntityReference)e.Attributes["cr884_recruitmentproject"]).Id))

                    {

                        continue;

                    }

                    else

                    {

                        x++;

                    }

                }

                string count = x.ToString();

                Entity ent = new Entity("cr884_recruitmentprojectinterviewers");

                ent.Id = context.PrimaryEntityId;

                ent["cr884_interviewnumber"] = count;

                service.Update(ent);

    

            }

        }   

    }

}

Im getting The given key was not present in the dictionary. can someone tell me why please ?

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: The given key was not present in the dictionary.

    Ralph Abou Jaoude,

    you can't think that the lookup is not null but somewhere is.

    Adding to a33ik response (that is enough to solve your problem) I explain you why.

    The error "The given key was not present in the dictionary." can be generated when accessing a dictionary with a key that doesn't exist. In your code the only dictionary is the "Attributes" property you are accessing of the Entity objects.

    in particular line

    Guid id = ((EntityReference)ret.Attributes["cr884_recruitmentproject"]).Id;

    and line

    if (!id.Equals(((EntityReference)e.Attributes["cr884_recruitmentproject"]).Id))

    one of the two lines is giving you the error (and if you debug you can which one, if the current entity or the one returned by the fetchxml you executed).

    to solve use the GetAttributeValue (as suggested by a33ik ) and check if the lookup is value is not null.

    If you still think that the lookup is not null, check the data and if all the records have the lookup cr884_recruitmentproject filled then open a support ticket to Microsoft. please let us know the result.

    Thanks

  • RE: The given key was not present in the dictionary.

    the lookup is not null

    the logic of my code and the syntax is right

    I am still not figuring out what the reason of this error

    Please advise

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: The given key was not present in the dictionary.

    Hello,

    I'm pretty sure that you have an error in one of 2 following lines:

    Guid id = ((EntityReference)ret.Attributes["cr884_recruitmentproject"]).Id;

    or

    if (!id.Equals(((EntityReference)e.Attributes["cr884_recruitmentproject"]).Id))

    I recommend using the following approach to reference the attribute:

    var reference = record.GetAttributeValue<EntityReference>("lookup field schema");

    If the lookup doesn't have any value (that's what is happening in your case I believe) reference will be null.

    So in order to avoid this issue you should handle that in a proper way.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans