Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

Duplicate detection based on date fields

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 !

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,962 Moderator on at
    RE: Duplicate detection based on date fields

    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

  • RE: Duplicate detection based on date fields

    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
    Bipin D365 28,962 Moderator on at
    RE: Duplicate detection based on date fields

    Hi,

    Are you getting any error ?

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

  • RE: Duplicate detection based on date fields

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

                   }

               }

           }

       }

    }

  • RE: Duplicate detection based on date fields

    That i am trying , don't have now .

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Duplicate detection based on date fields

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

  • RE: Duplicate detection based on date fields

    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
    a33ik 84,321 Most Valuable Professional on at
    RE: Duplicate detection based on date fields

    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.

  • RE: Duplicate detection based on date fields

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

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,570 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,683 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans