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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

The given key was not present in the dictionary.

(0) ShareShare
ReportReport
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 ?

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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.

  • Ralph Abou Jaoude Profile Picture
    35 on at

    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

  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    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

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 76

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans