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 ?

  • Verified answer
    Guido Preite Profile Picture
    54,077 Moderator 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

  • Ralph Abou Jaoude Profile Picture
    35 on at
    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
    84,329 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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,278 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,991 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans