RE: An unexpected error occurred from ISV code
Yes, I use that code to show customized code.
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
namespace TestPlugin
{
public class ImageDemo : IPlugin
{
private void ThrowPluginException()
{
throw new InvalidPluginExecutionException("The subgrid count is Zero");
}
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
// Create service with context of current user
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//create tracing service
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName.ToLower() != "incident") return;
try
{
Entity preImage = (Entity)context.PreEntityImages["Image"];
if (preImage.Contains("statecode"))
{
if (preImage.GetAttributeValue<OptionSetValue>("statecode").Value == 0)//if Pre image have priority = Low
{
if (entity.Contains("statecode") && entity.GetAttributeValue<OptionSetValue>("statecode").Value == 1)
{
if (preImage.Contains("new_subcount"))
{
Int32 countval = Convert.ToInt32(preImage.GetAttributeValue<Int32>("new_subcount").ToString());
if (countval == 0)
{
ThrowPluginException();
// throw new InvalidPluginExecutionException("The subgrid count is Zero");
}
}
}
}
}
}
catch (Exception ex)
{
throw new Exception("" + ex.Message);
}
}
}
}
}