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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

System.Collections.Generic.KeyNotFoundException err

(0) ShareShare
ReportReport
Posted on by 232

Hi Communities,

I created simple plugin : Whenever account updated that primarycontact fields also updated.

but I encountered the below business process error.

Please give me best solution.

16404.Untitled.png

coding:

namespace updatecontact
{
public class UpdateContact : IPlugin
{

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);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.InputParameters.Contains("Target") & context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];

if (entity.LogicalName == "account")

try
{

EntityReference contactId = (EntityReference)entity.Attributes["primarycontactid"];
Guid primaryContactGUID = contactId.Id;

Entity contact = new Entity("contact");

// Update the postal code attribute.
contact["address1_city"] = "Texas";

// The address 2 postal code was set accidentally, so set it to null.
contact["emailaddress1"] = "john334@gmail.com


contact["contactid"] = primaryContactGUID;
// Update the account.
service.Update(contact);
}


catch (Exception ex)
{
tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
throw;
}
}
}
}
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at
    RE: System.Collections.Generic.KeyNotFoundException err

    Change your update code as follows:

    Entity contact = new Entity("contact");

    contact.Id = primaryContactGUID;

    // Update the postal code attribute.

    contact["address1_city"] = "Texas";

    // The address 2 postal code was set accidentally, so set it to null.

    contact["emailaddress1"] = "john334@gmail.com

    // Update the account.

    service.Update(contact);

    That is usually the result you will get when using the actual contactid attribute. Use the Id of the entity for Update calls.

  • Dhanabal Profile Picture
    232 on at
    RE: System.Collections.Generic.KeyNotFoundException err

    Thanks Ravi

    It works fine now!

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: System.Collections.Generic.KeyNotFoundException err

    Hi Dhanabal,

    While updating the account, are you changing the primary contact value as well? If no then you won't be able to get the contact using the below statement-

    EntityReference contactId = (EntityReference)entity.Attributes["primarycontactid"];

    This is because the entity which you are retrieving from the Target "Entity entity = (Entity)context.InputParameters["Target"];" only contaions the fields which are updating. If the values are unchanged then it woun't come through.

    To overcome this limitation/ behavior, concept of images comes into picture. You can register preImage/ postImage which will have the field values even if they are not changed.

    You can read more about images here

    community.dynamics.com/.../pre-image-38-post-image-explained-33

    crmbook.powerobjects.com/.../plug-in-images-pre-vs-post

    community.dynamics.com/.../utilising-pre-post-entity-images-in-a-dynamics-crm-plugin

    In short, you can register preImage/ postImage and then retrieve the field values from their-

    -------------------------

    // PreImage Example

    Entity preImageEntity;

    if (context.PreEntityImages.Contains(“PreImage”) && context.PreEntityImages["PreImage"] is Entity)

    {

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

    EntityReference contactId = (EntityReference)preImageEntity.Attributes["primarycontactid"];

    }

    // PostImage Example

    Entity postImageEntity;

    if (context.PostEntityImages.Contains(“PostImage”) && context.PostEntityImages["PostImage"] is Entity)

    {

    postImageEntity= (Entity)context.PostEntityImages["PostImage"];

    EntityReference contactId = (EntityReference)postImageEntity.Attributes["primarycontactid"];

    }

    -------------------------

    Hope this helps

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans