Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

CRM Plugin - "The given key was not present in the dictionary."

Posted on by Microsoft Employee

Hi,

I'm trying to create a plugin which will take set an entity to a dynamic value based on the primary entity's Id. Essentially, when I create a new 'opportunity' entity, I want to update a text field containing a related url. I think this should be quite simple, and I can get this to work when creating a new 'opportunity', but for some reason running the workflow on an existing 'opportunity' gives the following error: "The given key was not present in the dictionary".

I have seen similar issues online, but nothing that quite solved this, can anyone help?

            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            OrganizationServiceContext ctx = new OrganizationServiceContext(service);

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

            {
                //retrieve Record GUID
                Guid recordId = (Guid)context.PrimaryEntityId;

                //Set Field Value = url + recordId
                targetEntity["web_url"] = "https://website.com/" + recordId;
                service.Update(targetEntity);

            }


*This post is locked for comments

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

    The reason is simple. Because it's not a plugin InputParameters will not contain Target that is caused exception you experienced. Instead of getting all the target you can use data from Activity context to grab entity name and id to update it.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM Plugin - "The given key was not present in the dictionary."

    Actually Andrew, sorry this solution (once I included that line) does work, it just needed that page to be saved for the field to populate. Thank you!
    Would you be able to explain briefly why this worked and the other didn't?
    Thanks again.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM Plugin - "The given key was not present in the dictionary."

    Hi Andrew, thanks for that. I assume you forgot to add in the line:

    Guid recordId = (Guid)context.PrimaryEntityId;

    Otherwise recordId is never declared, so I have added this in.

    This has gotten rid of the 'given key not present' error, but the field is still not populating.

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

    Hello,

    Following code should work fine:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Crm.Sdk.Messages;
    using System.Activities;
    using System.Collections.ObjectModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Workflow;
    using Microsoft.Xrm.Sdk.Client;
    
    namespace WebURLWorkflow
    {
        public class WebURL : CodeActivity
        {
            protected override void Execute(CodeActivityContext executionContext)
            {
                IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
                IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                var targetEntity = new Entity(context.PrimaryEntityName, context.PrimaryEntityId);
                targetEntity["web_url"] = "https://website.com/" + recordId;
                service.Update(targetEntity);
            }
        }
    }


  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM Plugin - "The given key was not present in the dictionary."

    Hi, thanks for the help, but that has not fixed it. I don't understand why it would pass to the field on create but not update.

  • Suggested answer
    Sai Krishna N Profile Picture
    Sai Krishna N 15 on at
    RE: CRM Plugin - "The given key was not present in the dictionary."

    Hey, 

    Could you please verify if you're passing value to the field "web_url" in update operation because in InputParameters["Target"] we get the fields which are modified only during the update operation.

    Could you try declaring the new instance of Entity object before setting the value to the field "web_url" like below:

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

    //retrieve RecordGUID

    Guid recordId = (Guid)context.PrimaryEntityId;

    //Set Field Value = url + recordId

    var targetEntity1=new Entity();

    targetEntity1.Id=recordId;

    targetEntity1["web_url"] = "https://website.com/" + recordId;

    service.Update(targetEntity1); 

    Hope this helps you.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CRM Plugin - "The given key was not present in the dictionary."

    Yes, sorry you are correct, it is a custom workflow I am building. 'web_url' is an attribute of the 'targetEntity'.

    Here is the full code, but there is not much more to add.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Crm.Sdk.Messages;
    using System.Activities;
    using System.Collections.ObjectModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Workflow;
    using Microsoft.Xrm.Sdk.Client;
    
    namespace WebURLWorkflow
    {
        public class WebURL : CodeActivity
        {
            protected override void Execute(CodeActivityContext executionContext)
            {
    
                //Create the context
                IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
                IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                OrganizationServiceContext ctx = new OrganizationServiceContext(service);
    
                Entity targetEntity = (Entity)context.InputParameters["Target"];     
    
                {
                    //retrieve Record GUID
                    Guid recordId = (Guid)context.PrimaryEntityId;
    
                    //Set Field Value = url + recordId
                    targetEntity["web_url"] = "https://website.com/" + recordId;
                    service.Update(targetEntity);
                }
            }
        }
    }
    


     

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

    Nope, it's late bound.

  • gdas Profile Picture
    gdas 50,085 on at
    RE: CRM Plugin - "The given key was not present in the dictionary."

    My concern is what is the type of outcome "targetEntity "  here below code if I consider as plugin perspective -it is early bound object right?

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

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

    For early bound there should be some class mentioned other then "Entity" like

    var target = (Account)context.InputParameters["Target"];

    So for me it's still not clear - where did you see Early Bound?

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

Featured topics

Product updates

Dynamics 365 release plans