Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

System.Collections.Generic.KeyNotFoundException err

Posted on by 230

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

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 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
    Dhanabal 230 on at
    RE: System.Collections.Generic.KeyNotFoundException err

    Thanks Ravi

    It works fine now!

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 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

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