Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Plugin Error

Posted on by Microsoft Employee

Hi

My logic is i have parent entity and child entity parent has two Fields value and units child have Total field when i enter value"10" and Units "10" i need to update Total field "100" in child entity 

ERROR -:System.Collections.Generic.KeyNotFoundException: 'The given key was not present in the dictionary 

please somebody give me help.

Thanks ,

Naresh

 

CODE:

using System;
using Microsoft.Xrm.Sdk;

namespace Multipication
{
public class Update : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{

ITracingService traceService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.

IPluginExecutionContext context =
(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

// Get a reference to the Organization service.
IOrganizationServiceFactory factory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
traceService.Trace("Entered in to the target entity");
Entity erp = (Entity)context.InputParameters["Target"];
//Instead of getting entity from Target, we use the Image
traceService.Trace(erp.LogicalName);
// Entity entity = context.PostEntityImages["PostImage"];

var rate = erp.GetAttributeValue<Money>("new_rate").Value;

//var rate = Money(rate * decimal.Parse(units.ToString()));

traceService.Trace("This is rate value", rate);
var units = erp.GetAttributeValue<string>("new_units").ToString();
traceService.Trace(units.ToString());
// EntityReference parent = (EntityReference)erp.Attributes["new_partnerid"];
EntityReference parent = (EntityReference)erp.Attributes["new_privateid"];
traceService.Trace("Parent guid", parent.Id);
//Multiply
Money total = new Money(rate * decimal.Parse(units.ToString()));
traceService.Trace("This is a total", total);
//Set the update entity
Entity parententity = new Entity("new_privateengagement");
parententity.Id = parent.Id;
parententity.Attributes["new_total"] = total;
traceService.Trace("Enterd a line 40");
//Update
service.Update(parententity);

}

}

}
}

*This post is locked for comments

  • Verified answer
    Shaminderpal Singh Profile Picture
    Shaminderpal Singh 1,565 on at
    RE: Plugin Error

    So,in order to get this right ,we need to follow below design

    1)Plugin on create of child entity (Pre operation)

    2)No need of images.Get the parent entity id from entity reference as you are doing

    3)Retrieve the two fields  value and units from parent entity.

    4)Calculate total and assign it to total field

    5)No need of update

    -Shaminder

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plugin Error

    Hi shaminderpal

    when i create a record i need to run logic

  • Suggested answer
    Shaminderpal Singh Profile Picture
    Shaminderpal Singh 1,565 on at
    RE: Plugin Error

    Hi Naresh,

    From your code,it seems your are doing reverse,you are updating parent entity from your child entity as your parent entity doesnt have a field with total schema ,its giving an error that key is not found in dictionary.

    Can you tell when you want to run this logic,on create of child entity?

    -Shaminder

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