HI Andrew,
Please find the code below
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
namespace DuplicateRecord
{
public class Show : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Entity show= null;
string enddate = string.Empty;
string startdate = string.Empty;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) ;
{
show = (Entity)context.InputParameters["Target"];
}
if ((show.Attributes.Contains("new_name") && show.Attributes["new_name"] != null) && (show.Attributes.Contains("new_showenddate") && show.Attributes["new_showenddate"] != null) && ( show.Attributes.Contains("new_showstartdate") && show.Attributes["new_showstartdate"]!=null))
{
startdate = ((DateTime)show.Attributes["new_showstartdate"]).ToString("yyyy-MM-dd");
enddate = ((DateTime)show.Attributes["new_showenddate"]).ToString("yyyy-MM-dd");
string showQuery = $@"<fetch version='1.0' output-format='xml - platform' mapping='logical' distinct='false'>
< entity name = 'new_show' >
< filter type = 'and'>
< condition attribute = 'new_name' operator= 'eq' value='{(string)show.Attributes["new_name"]}'/>
< condition attribute = 'new_showstartdate' operator= 'on-or-after' value='{startdate}' />
< condition attribute = 'new_showenddate' operator= 'on-or-before' value='{enddate}' />
</ filter >
</ entity >
</ fetch >";
EntityCollection retrivedShowRecord = service.RetrieveMultiple(new FetchExpression(showQuery));
if (retrivedShowRecord.Entities.Count > 0)
{
throw new InvalidPluginExecutionException("Duplicate show Record Detected");
}
}
}
}
}