Hi,
I'm trying to create a plugin that will update the annual revenue of a parent Account of a Contact with a static value defined in a plugin.
This is just a throwaway plugin to test behavior of a scenario we are running (the scenario is a little nonsensical but there is reasoning behind it). The plugin fires on update of the email field on the Contact.
As a non-developer I've used the Microsoft documentation and a couple of blog posts to come up with the following code.
I've tried an number of different versions of the code but always get the error "The given key was not present in the dictionary". From what I understand, this would suggest the Target or Attributes I'm accessing are not present in the dictionary. I can't understand why that would be the case. If it's relevant, the Annual Revenue field currently contains data on the Parent Account.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
namespace CalculatedFieldPlugin
{
public class CalculationTest : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the tracing service
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
EntityReference a = (EntityReference)entity.Attributes["parentcustomerid"];
// Obtain the organization service reference which you will need for
// web service calls.
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
// Plug-in business logic goes here.
//updating the field on the account
Entity acc = new Entity("account");
acc.Id = a.Id;
acc.Attributes.Add("revenue", new Money(10));
service.Update(acc);
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
}
catch (Exception ex)
{
tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
throw;
}
}
}
}
}
*This post is locked for comments
I have the same question (0)