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 CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by

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

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

    Hello,

    It looks like your code is written for Custom workflow activity and not for plugin. Can you please provide your full code?

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    This message says that the property you have declared to update is not found in the target entity. Make sure your target enity contains the attributes web_url

    Try with this -

              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
                    if (targetEntity.Attributes.Contains("web_url"))
                    {
                        targetEntity["web_url"] = "https://website.com/" + recordId;
                        service.Update(targetEntity);
                    }
    
                }


  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Goutam,

    I'm really sorry but your reply doesn't make sense. targetEntity["web_url"] will throw an exception when code tries to get value and key is not there. When code sets the value no check is required.

    I believe problem is in line

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

    because context is IWorkflowContext and not IPluginExecutionContext but I want to check full code first.

  • gdas Profile Picture
    50,091 Moderator on at

    Andrew ,

    My intention was to point out the issue  based on the code shared here to troubleshoot . I believe you are aware that if the object don't have the attributes (web_url ) in the entity object and trying to execute the code targetEntity["web_url"] then this obvious that it will give error "The given key was not present in the dictionary". I believe this is C# error.

    I agreed with you that here we need full code to make it work.

    Thanks for your clarification .

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    I'm aware. Correct me if I'm wrong if code and explanations are correct

    var record = new Entity();

    record["fieldname"] = "test";//No exception - if there is fieldname it will be replaced with "test" value, if there is no - key "fieldname" will be added with "test" value.

    var value = record["otherfieldname"];//Exception because otherfieldname is not present.

    That's what I meant saying your change makes no sense.

  • gdas Profile Picture
    50,091 Moderator on at

    Hi Andrew,

    Correct me if I am wrong.

    This is possible if you are using late bound entity type , but the code shared here in the post is an early bound entity type so there is no chances to replace the attributes name .

    var record = new Entity();

    record["fieldname"] = "test"; // This is correct for late bound entity type.

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    All the code in this post is late bound. Nobody provided any example of early bound. What you've told is correct - I answered your question. Now please tell me if my previous reply is correct or not.

  • gdas Profile Picture
    50,091 Moderator on at

    Hi Andrew ,

    I already answered that for late bound entity type your code is correct , but here in the below code targetentity has already filled with early bound entity type in the below code  if I am not wrong.

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

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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?

  • gdas Profile Picture
    50,091 Moderator on at

    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"];

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans