Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Pluggin code will not work getting a

Posted on by Microsoft Employee

Hi everyone,

Im trying to write the post images value of a field to another field once contact record is updated.

I get the following error

Unexpected exception from plug-in (Execute): ClassLibrary1.Class1: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. 

Below is my code 

ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];



string pretopic = string.Empty; string posttopic = string.Empty;

if (context.PreEntityImages.Contains("LeadTopicImage") && context.PreEntityImages["LeadTopicImage"] is Entity)
{

Entity preMessageImage = (Entity)context.PreEntityImages["LeadTopicImage"];
pretopic = (String)preMessageImage.Attributes["jobtitle"];

}
if (context.PostEntityImages.Contains("LeadTopicImage") && context.PostEntityImages["LeadTopicImage"] is Entity)

{

Entity postMessageImage = (Entity)context.PostEntityImages["LeadTopicImage"];
posttopic = (String)postMessageImage.Attributes["jobtitle"];

}

context.InputParameters.Contains("spousesname");
entity.Attributes.Add("spousesname",posttopic);

*This post is locked for comments

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Pluggin code will not work getting a

    Hi Jonathan,

    I understand that you wrote in the beginning to validate the entity from the context input parameter( context.InputParameters.Contains("Target") ) , that is correct .

    I can see again you are checking the the attributes in the context input parameter , you can not check attributes in the input parameter like below . If you are looking for attributes you should do the contains in the entity /entityreference type only instead of input parameter.

    context.InputParameters.Contains("spousesname");

    Now question is in which entity spousesname attributes exists ?

    If the attributes in the same entity , you can simply get the  entity and set the entity value like below- 

    Entity record = (Entity)context.InputParameters["Target"]; // I believe you have already got the entity in the above lines
    record["attributename"] = /* any value */;


    If the spousesname  attributes belongs to another entity you need to create the entity separately,

    Entity account = new Entity("EnityName"); // Replace EntityName
    
    account["spousesname"] = "Sample text";

    Hope this helps.

  • Suggested answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: Pluggin code will not work getting a

    Hi,

    Do you have registered pre image step and its name matching with above(i.e. preMessageImage - its case sensitive). if yes, Did You selected Jontitle in preimage step? 

    Please check, that should solve your issue.

    - Sreevalli

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Pluggin code will not work getting a

    Hello,

    It is safer to use GetAttributeValue instead of referencing attribute directly because if there is no such attribute in your entity - you will get KeyNotFound Exception so instead of

    pretopic = (String)preMessageImage.Attributes["jobtitle"];

    use

    pretopic = preMessageImage.GetAttributeValue<string>("jobtitle");

    the same for the posttopic.

    Also what does

    context.InputParameters.Contains("spousesname");

    this line mean?

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans