Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Plugin to prevent Duplication

Posted on by 125

Hi All,

I need to create a plugin to prevent the duplication but with a different Criteria The scenario :

I have Maintenance department and service department so fro example :

Contract_number           Department

1002                               Maintenance

1002                               Service

This is right

Contract_number           Department

1002                               Maintenance

1002                               Maintenance

This is wrong

So the following code will prevent the duplication for all contract numbers how can i convert it to be for the departments level The Code:

if (contract.Contains("new_name"))
              contractNumber = (string)contract.Attributes["new_name"];
              else if (contractImg.Contains("new_name"))
                  contractNumber = (string)contractImg.Attributes["new_name"];
              QueryExpression qExp = new QueryExpression();
              EntityCollection eCol = new EntityCollection();
 
              qExp.EntityName = "new_contract";
              qExp.ColumnSet = new ColumnSet("new_name");
              qExp.Criteria.AddCondition("new_name", Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal,(contractNumber));
              eCol = service.RetrieveMultiple(qExp);
              
              if (eCol.Entities.Count > 1)
              {
                 throw new InvalidPluginExecutionException("Contract Number already exists ");
          
              }

*This post is locked for comments

  • Suggested answer
    Abed Haniyah Profile Picture
    Abed Haniyah 4,285 on at
    RE: Plugin to prevent Duplication

    The default filter is AND, If you need to change it then you can use Filter Expression :)

  • Mena_Khalaf Profile Picture
    Mena_Khalaf 125 on at
    RE: Plugin to prevent Duplication

    Thanks, but you don't use filter1 the and operator ?

  • Verified answer
    Abed Haniyah Profile Picture
    Abed Haniyah 4,285 on at
    RE: Plugin to prevent Duplication

    int DepartmentValue;

    if (contract.Contains("new_name"))

                 contractNumber = (string)contract.Attributes["new_name"];

                 else if (contractImg.Contains("new_name"))

                     contractNumber = (string)contractImg.Attributes["new_name"];

    if (contract.Contains("new_department"))  //Get the option set value for the department field 

                 DepartmentValue = contract.GetAttributeValue<OptionSetValue>("new_department").Value;

    else if (contractImg.Contains("new_department"))

                    DepartmentValue = contractImg.GetAttributeValue<OptionSetValue>("new_department").Value;

                 QueryExpression qExp = new QueryExpression();

                 EntityCollection eCol = new EntityCollection();

                 qExp.EntityName = "new_contract";

                 qExp.ColumnSet = new ColumnSet("new_name");

    FilterExpression filter1 = new FilterExpression(LogicalOperator.And);

                 qExp.Criteria.AddCondition("new_name", Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal,(contractNumber));

                 qExp.Criteria.AddCondition("new_department", Microsoft.Xrm.Sdk.Query.ConditionOperator.Equal,(DepartmentValue)); // add condition to check the department

    eCol = service.RetrieveMultiple(qExp);

                 if (eCol.Entities.Count > 0)

                 {

                    throw new InvalidPluginExecutionException("Contract Number already exists ");

                 }

  • Mena_Khalaf Profile Picture
    Mena_Khalaf 125 on at
    RE: Plugin to prevent Duplication

    Yes the Department field is an optionset

  • Mena_Khalaf Profile Picture
    Mena_Khalaf 125 on at
    RE: Plugin to prevent Duplication

    And the Department Field is new_Type

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans