HI Everyone,
I have custom entity with 3 attributes that are text field and two date fields start and end date .
i need to restrict creation of duplicate records based on all 3 fields and new record should not lie between the date range of existing one so how to achieve this please give solution .
thank you in advance !
Hi,
You should generate fetchXml using fetchXml builder tool in xrmtoolbox
Also, try to debug your code to see the generated fetchXml for syntax
yes ,
Exception Message: Invalid XML.
ErrorCode: -2147220991
HexErrorCode: 0x80040201
ErrorDetails:
ApiExceptionSourceKey: Api/PrePipeline
ApiStepKey: 00000000-0000-0000-0000-000000000000
ApiDepthKey: 1
ApiActivityIdKey: 6c4375b8-5d99-4b2a-84c3-c35e86651678
ApiPluginSolutionNameKey: System
ApiStepSolutionNameKey: System
ApiExceptionCategory: ClientError
ApiExceptionMessageName: InvalidXml
ApiExceptionHttpStatusCode: 400
HelpLink: go.microsoft.com/.../
TraceText:
[DuplicateRecord: DuplicateRecord.Show]
[4208fb01-21ee-ec11-bb3d-0022480a615d: DuplicateRecord.Show: Create of new_show]
ActivityFeeds.Plugins.PluginBase.Execute(): Configuration is invalid. Skipping ActivityFeeds plugin execution (ConfigParam is null)
ActivityFeeds.Plugins.PluginBase.Execute(): Configuration is invalid. Skipping ActivityFeeds plugin execution (ConfigParam is null)
ActivityFeeds.Plugins.PluginBase.Execute(): Configuration is invalid. Skipping ActivityFeeds plugin execution (ConfigParam is null)
ActivityFeeds.Plugins.PluginBase.Execute(): Configuration is invalid. Skipping ActivityFeeds plugin execution (ConfigParam is null)
Activity Id: 44c768f8-19de-425e-a762-9edbab5fa22e
Hi,
Are you getting any error ?
Can you please tell the Data type behaviour of date & time field is set to Date only?
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");
}
}
}
}
}
That i am trying , don't have now .
Where is the code of the plugin since you want to use it for duplicate detection?
string fetchquery = @"<fetch version='1.0' output-format='xml - platform' mapping='logical' distinct='false'>
< entity name = 'new_show' >
< order attribute = 'new_name' descending = 'false' />
< filter type = 'and'>
< condition attribute = 'new_name' value = 'x' operator= 'eq' />
< condition attribute = 'new_showstartdate' value = 'today' operator= 'on-or-after' />
< condition attribute = 'new_showenddate' value = 'today' operator= 'on-or-before' />
</ filter >
</ entity >
</ fetch >";
i have this fetch xml
Hello,
You can use FetchXml (and querying in general) to validate the data. Start writing the plugin, bring what you got here and I will help you.
Can i use fetch xml on dates like on or after start date and on or before end date in plugin to achieve this
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156