using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace isvalid.Plugins
{
public class productopp : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.It Contains the information Current Loged in User like user id etc.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = (IOrganizationService)factory.CreateOrganizationService(context.UserId);
ITracingService TracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
//InputParameters Contains all the information of Message request send by the user cliend site
// Target is checking two thing is this Targeting(have primary entity or not) any entiy or not and what evere we are target is entity or not
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
//Obtain the target entity from the input parameters
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "opportunityproduct")
return;
try
{
string str = entity.FormattedValues["new_isvalid"].ToString();
if (str == "Yes")
{
throw new InvalidPluginExecutionException("The Product is valid.");
}
else
{
throw new InvalidPluginExecutionException("The Product is Invalid.");
}
}
catch (Exception ex)
{
TracingService.Trace("MyPlugin:{0}", ex.ToString());
}
}
}
}
}

Report
All responses (
Answers (