Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

i am getting this error given key is not found

Posted on by Microsoft Employee

I am using images can anyone help me

public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

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

IOrganizationService service = (IOrganizationService)servicefactory.CreateOrganizationService(context.UserId);

if(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
if (context.MessageName.ToLower() == "update")
{
Entity contact = context.InputParameters["Target"] as Entity;
Entity PreImagecontact = context.PreEntityImages["Preimage"] as Entity;

if (contact.Attributes["fullname"].ToString() == PreImagecontact.Attributes["fullname"].ToString())
{
throw new InvalidPluginExecutionException("jobtitle is not changed");
}
else if (contact.Attributes["fullname"].ToString() != PreImagecontact.Attributes["fullname"].ToString())
{
throw new InvalidPluginExecutionException("jobtitle is changed");
}
}

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: i am getting this error given key is not found

    thank u

  • Verified answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: i am getting this error given key is not found

    public void Execute(IServiceProvider serviceProvider)
    {
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    IOrganizationService service = (IOrganizationService)servicefactory.CreateOrganizationService(context.UserId);
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
    if (context.MessageName.ToLower() == "update")
    {
    Entity contact = context.InputParameters["Target"] as Entity;
    Entity PreImage = (Entity)context.PreEntityImages["Preimage"];
    if (contact.Attributes["fullname"].ToString() == PreImage.Attributes["fullname"].ToString())
    {
    throw new InvalidPluginExecutionException("jobtitle is not changed");
    }
    else if (contact.Attributes["fullname"].ToString() != PreImage.Attributes["fullname"].ToString())
    {
    throw new InvalidPluginExecutionException("jobtitle is changed");
    }
    }
    }
    }

    Can you please  copy above code and update your Plugin once

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: i am getting this error given key is not found

    i changed the line

  • Verified answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: i am getting this error given key is not found

    Did you change above mentioned line or not? The problem here is you didn't register Image with the name what you provided in code.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: i am getting this error given key is not found

    [Microsoft.Crm.ObjectModel: Microsoft.Crm.ObjectModel.SyncWorkflowExecutionPlugin]

    [e8a8f4cd-d2a3-e611-811b-c4346bdd21d1: ]

    Starting sync workflow 'Set Managing Partner on creation of contact via portal', Id: e0a8f4cd-d2a3-e611-811b-c4346bdd21d1

    Entering UpdateStep1_step:

    Sync workflow 'Set Managing Partner on creation of contact via portal' terminated with error 'Unexpected exception from plug-in (Execute): images.images: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.'

    </TraceText>

    </OrganizationServiceFault>

  • Verified answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: i am getting this error given key is not found

    Observe the difference

    Entity PreImage = (Entity)context.PreEntityImages["PreImage"];

     

    4265.Screenshot_5F00_1.png

     


    I told you, this is case sensitive. Either you need to change above line with

    Entity PreImage = (Entity)context.PreEntityImages["Preimage"];

     

    or else you need to update your PreImage.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: i am getting this error given key is not found

    public void Execute(IServiceProvider serviceProvider)
    {
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

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

    IOrganizationService service = (IOrganizationService)servicefactory.CreateOrganizationService(context.UserId);

    if(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
    if (context.MessageName.ToLower() == "update")
    {
    Entity contact = context.InputParameters["Target"] as Entity;
    //Entity PreImagecontact = context.PreEntityImages["Preimage"] as Entity;
    Entity PreImage = (Entity)context.PreEntityImages["PreImage"];

    if (contact.Attributes.Contains("fullname"))
    {
    if (contact.Attributes["fullname"].ToString() == PreImage.Attributes["fullname"].ToString())
    {
    throw new InvalidPluginExecutionException("jobtitle is not changed");
    }
    }
    else if (contact.Attributes["fullname"].ToString() != PreImage.Attributes["fullname"].ToString())
    {
    throw new InvalidPluginExecutionException("jobtitle is changed");
    }

    this what i am doing , still i am getting error

    1411.11.JPG1411.11.JPG

  • Suggested answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: i am getting this error given key is not found

    I just used your code only. No magic. I just copied your code and added references and Register Step On Post update operation of Contact Entity and Registered Preimage.

     


    That's it.

    Community-Screen-Image-error.png

    Community-Screen-Image-error.png

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: i am getting this error given key is not found

    i have given the same name, but still i am getting error,which code that you used exactly can u tell me i tried everything

  • Suggested answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: i am getting this error given key is not found

    Hi avinay,

    I tried your code and it is working as expected. I didn't get any error regarding given Key.

    Hope you register Image with same name as you gave in your code.

    Note : Image name is case sensitive in Plugin registration Tool.

    Community-Screen-Image.png

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans