I wrote the following plug-in for ms crm 2016 that's detect duplication.
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = (IOrganizationService)serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
var entity = (Entity) context.InputParameters["Target"];
var name = entity.GetAttributeValue<string>("name");
var opportunityCompany = new QueryExpression
{
EntityName = entity.LogicalName,
ColumnSet = new ColumnSet("name")
};
opportunityCompany.Criteria.AddCondition("name", ConditionOperator.Equal, name);
var retrieveAccount = service.RetrieveMultiple(opportunityCompany);
//If the retrieved Company Count Greater Than 1 , following Error Message Throw
if (retrieveAccount.Entities.Count > 1)
{
throw new InvalidPluginExecutionException("Following Record with Same Name is Exists");
}
}
}
This works fine but in case of duplicate i want to end record creation without exception and without message on page. How can i do this in plug-in? Some people talks to me that i can implement this in CodeActivity. How can i implement CodeActivity in ms crm plug-in?
Or rather I want to stop the plug-in so that the "user" did not know about it. How better do it?