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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Delete duplication recorders depend on my beefiness rule

(0) ShareShare
ReportReport
Posted on by

Hi All  ihave the same problem but i want to apply SuppressDuplicateDetection  in my costume business logic

between Agreement, AgreementBookingSetup, AgreementBookingDates Entities 

for each Agreement have a multiple AgreementBookingSetup and AgreementBookingSetup have a multiple AgreementBookingDates 

to explaince the problem :

 7658.BookingDatesDublicates.jpg

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    T.I.A Profile Picture
    1,760 on at
    RE: Delete duplication recorders depend on my beefiness rule

    Could you use a plugin to make sure your conditions are met, then throw a InvalidPluginExecutiionException? I'm answering from what I understood from your post

  • Community Member Profile Picture
    on at
    RE: Delete duplication recorders depend on my beefiness rule

    My Plugin

    using System;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;


    namespace CFM.UpdateCustomerAsset.BookingDate
    {
    public class SuppressionBookingDates : IPlugin
    {
    IOrganizationService service = null;

    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));
    service = serviceFactory.CreateOrganizationService(context.UserId);


    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
    Entity entity = (Entity)context.InputParameters["Target"];

    if (entity.LogicalName == "msdyn_agreementbookingdate")
    {

    // tracingService.Trace("AdvancedPlugin: Verifying the client is not offline.");
    tracingService.Trace("Plugin Entered Agreementbookingdate");

    if (entity.Contains("msdyn_agreement"))
    {
    //4
    tracingService.Trace("Entered Agreement.");

    if ((entity.Attributes["msdyn_agreement"]) != null)
    {

    //5
    Guid agreement = ((EntityReference)entity.Attributes["msdyn_agreement"]).Id;

    // get booking dates with the same agreement and does not have the same id as the entity
    var fetchXmlDates = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
    fetchXmlDates += "<entity name='msdyn_agreementbookingdate'>";
    fetchXmlDates += "<attribute name='msdyn_bookingsetup' />";
    fetchXmlDates += "<attribute name='msdyn_bookingdate' />";
    fetchXmlDates += "<attribute name='msdyn_agreement' />";
    fetchXmlDates += "<attribute name='msdyn_agreementbookingdateid' />";
    fetchXmlDates += "<order attribute='msdyn_bookingdate' descending='true' />";
    fetchXmlDates += "<filter type='and'>";
    fetchXmlDates += "<condition attribute='msdyn_agreement' operator='eq' uitype='msdyn_agreement' value='" + agreement + "' />";
    fetchXmlDates += "<condition attribute='msdyn_agreementbookingdateid' operator='ne' value='" + entity.Id + "' />";
    fetchXmlDates += "</filter>";
    fetchXmlDates += "<link-entity name='msdyn_agreementbookingsetup' from='msdyn_agreementbookingsetupid' to='msdyn_bookingsetup' alias='bookRec'>";
    fetchXmlDates += "<attribute name='new_bookingrecurring' />";
    fetchXmlDates += "<attribute name='new_customerasset' />";
    fetchXmlDates += "<filter type='and'>";
    fetchXmlDates += "<condition attribute='new_bookingrecurring' operator='not-null' />";
    fetchXmlDates += "</filter>";
    fetchXmlDates += "</link-entity>";
    fetchXmlDates += "</entity>";
    fetchXmlDates += "</fetch>";

    var resultDates = service.RetrieveMultiple(new FetchExpression(fetchXmlDates));

    tracingService.Trace("Completed FetchXml");

    try
    {
    tracingService.Trace("Entered Try block");

    if (resultDates.Entities.Count > 1)
    {
    foreach (Entity bookingDate in resultDates.Entities)
    {
    tracingService.Trace("Booking Date", bookingDate.Id);

    if (entity.Contains("msdyn_bookingdate"))
    {

    // check the booking value option set
    OptionSetValue RelatEntityBookingRec = GetRelatedAtt(bookingDate);
    OptionSetValue entityBookingRec = GetRelatedAtt(entity);

    tracingService.Trace("Optionset RelateentitybookingRec");

    // get the numbered values to compare between fields and suppress dates
    var entityBookNum = entityBookingRec.Value;
    var fetchBookNum = RelatEntityBookingRec.Value;


    // get month of the date to compare
    var bookingFetchDate = (DateTime)bookingDate.Attributes["msdyn_bookingdate"];
    var entityDate = (DateTime)entity.Attributes["msdyn_bookingdate"];
    var bookingFetchCA = "";
    var entityCA = "";

    tracingService.Trace("BookingFetch CA");

    bookingFetchCA = bookingDate.Attributes.Contains("new_customerasset") ? (string)bookingDate.Attributes["new_customerasset"] : String.Empty;
    entityCA = entity.Attributes.Contains("new_customerasset") ? (string)entity.Attributes["new_customerasset"] : String.Empty;

    tracingService.Trace("entity CA");

    if (entity.Id != bookingDate.Id)
    {

    if (bookingFetchDate == entityDate)// && bookingFetchCA == "84f07501-9b39-e911-a960-000d3a4647a5")
    {

    if (bookingFetchCA == entityCA)
    {

    foreach (Entity item in resultDates.Entities)
    {

    if (entityBookNum < fetchBookNum)
    {
    // delete the current entity
    var newEntity = new EntityReference(entity.LogicalName, entity.Id);

    service.Delete(entity.LogicalName, entity.Id);

    }
    else if (entityBookNum > fetchBookNum)
    {
    // delete query
    var newEntity = new EntityReference(bookingDate.LogicalName, bookingDate.Id);

    service.Delete(newEntity.LogicalName, newEntity.Id);


    }
    }
    }
    }
    }
    }
    }
    }

    }
    catch (Exception e)
    {

    throw new InvalidPluginExecutionException(e.ToString());
    }


    }

    }


    }

    }
    }

    // method to retrieve the attribute from related entity
    private OptionSetValue GetRelatedAtt(Entity target)
    {
    OptionSetValue att = null;
    // check entity is (only need this if multiple steps/entities are registered for this plugin)
    if (target.LogicalName.Equals("msdyn_agreementbookingdate"))
    {

    Guid x = ((EntityReference)target.Attributes["msdyn_bookingsetup"]).Id;

    // get a reference to the entity
    EntityReference relatedEntityRef = new EntityReference("msdyn_agreementbookingsetup", x);

    // customer can be either an account or a contact. check for account
    if (relatedEntityRef.LogicalName.Equals("msdyn_agreementbookingsetup"))
    {
    Guid relatedEntityId = relatedEntityRef.Id; // get the guid of the related entity

    // get the attribute of the related entity


    Entity relatedEntity = service.Retrieve("msdyn_agreementbookingsetup", relatedEntityId, new ColumnSet("new_bookingrecurring")); // query crm for the related entity based on it's id
    if (relatedEntity != null)
    {
    att = relatedEntity.GetAttributeValue<OptionSetValue>("new_bookingrecurring");
    }

    }
    }

    return att;
    }


    }
    }

    In Complete Source Code

    Regrades

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans