I am new to plugins and I am trying to create a simple update plugin to get my project started. All I want to do is update the "description" field on contact with a string "test". I got this to work, but then I changed something in the code to try something new. Now I can't get it to work again.
I am using the Plugin Registration Tool and have set up the step like in the picture. When I do some changes in the "crmn_ourdescription" field nothing seems to happen. I have tried both pre- and post-operation.
What is wrong here?
using System;
using Microsoft.Xrm.Sdk;
namespace CalculateScorePlugin
{
public class CalculateScore : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory servicefactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService client = servicefactory.CreateOrganizationService(context.UserId);
{
if (context.InputParameters.Contains("Target"))
{
if (context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "contact")
{
if (entity.Attributes.Contains("description"))
{
try
{
//entity["crmn_ourdescription"] = "Test";
entity.Attributes["description"] = "Test";
//entity.Attributes["crmn_ourdescription"] = "Tester";
client.Update(entity);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("An error occured in the plugin:", ex);
}
}
}
else return;
}
}
}
}
}
}
↧
Update plugin not working
↧