Quantcast
Viewing all articles
Browse latest Browse all 79901

Plugin Error

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);

}

}

}
}


Viewing all articles
Browse latest Browse all 79901

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>