web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Duplicate detection based on date fields

(0) ShareShare
ReportReport
Posted on by 55

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 !

I have the same question (0)
  • Beginner Developer Profile Picture
    55 on at

    Can i use fetch xml on dates like on or after start date and on or before end date in plugin to achieve this

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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.

  • Beginner Developer Profile Picture
    55 on at

    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

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Where is the code of the plugin since you want to use it for duplicate detection?

  • Beginner Developer Profile Picture
    55 on at

    That i am trying , don't have now .

  • Beginner Developer Profile Picture
    55 on at

    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");

                   }

               }

           }

       }

    }

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Are you getting any error ?

    Can you please tell the Data type behaviour of date & time field is set to Date only?

  • Beginner Developer Profile Picture
    55 on at

    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

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    You should generate fetchXml using fetchXml builder tool in xrmtoolbox

    https://fetchxmlbuilder.com/

    Also, try to debug your code to see the generated fetchXml for syntax

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans