locked
Delete Duplicate detection rules RRS feed

  • Question

  • Hi All,

    Is it possible to delete duplicate detection rules programatically from MSCRM? I am trying to retrieve the duplicate rules associated with some entities and then wanted to delete them. I tried many ways but no luck.... could anyone pls help me or guid me on how to proceed with it?
     Below is the sample code... thru which am trying to retrieve the duplicate rules:

    CrmService

     

    service = new CrmService();

    service.CrmAuthenticationTokenValue = token;

    service.Credentials = System.Net.

    CredentialCache.DefaultCredentials;

     

     

    // Create the ColumnSet that indicates the properties to be retrieved.

     

    ColumnSet cols = new ColumnSet();

     

    // Set the properties of the ColumnSet.

    cols.Attributes =

    new string[] { "DuplicateRuleId" };

     

    // Create the ConditionExpression.

     

    ConditionExpression condition = new ConditionExpression();

     

    // Set the condition for the retrieval to be when the contact's address' city is Sammamish.

    condition.AttributeName =

    "BaseEntityName";

    condition.Operator =

    ConditionOperator.In;

    condition.Values =

    new string [] {"gsp_opportunityteam","gsp_accountteam","gsp_contactteam"};

     

    // Create the FilterExpression.

     

    FilterExpression filter = new FilterExpression();

     

    // Set the properties of the filter.

    filter.FilterOperator =

    LogicalOperator.And;

    filter.Conditions =

    new ConditionExpression[] {condition};

     

    // Create the QueryExpression object.

     

    QueryExpression query = new QueryExpression();

     

    // Set the properties of the QueryExpression object.

    query.EntityName =

    EntityName.duplicaterule.ToString();

    query.ColumnSet = cols;

    query.Criteria = filter;

     

     

     

    RetrieveMultipleRequest RetRules = new RetrieveMultipleRequest();

    RetRules.Query = query;

     

    RetrieveMultipleResponse DelRules = (RetrieveMultipleResponse)service.Execute(RetRules);


    Is this code correct? Pls do guide me....


    Thanks.

    Tuesday, September 22, 2009 4:52 AM

Answers

All replies

  • Basically I want to delete some duplicate rule detection.... pls guide me on how to proceed...
    Tuesday, September 22, 2009 5:06 AM
  • Try using TargetDeleteDuplicateRule Class (CrmService). and DeleteRequest class. Its 3 in the night so I would try to provide some sample code in my next post.
    Sunday, October 11, 2009 7:54 AM
  • Following is the example code which retrives all dupliate detection rules and deletes them in CRM system.

    make sure you change servicename and credential according to your CRM details....also change the filter as current filter retrives all duplicate detection rules and deletes them..

    CrmService incService = new CrmService();
    
    CrmAuthenticationToken token = new CrmAuthenticationToken();
    token.AuthenticationType = 0;
    token.OrganizationName = <<ORGNAME>>;
    
    
    incService.Url = <<CRM Service URL>>
    incService.CrmAuthenticationTokenValue = token;
    // default or network based on your requirement 
    //incService.Credentials = System.Net.CredentialCache.DefaultCredentials;
    
    incService.Credentials = new System.Net.NetworkCredential(<<USERNAME>>,<<PASSWORD>>,<<DOMAIN>>);
    incService.PreAuthenticate = false;
    incService.UnsafeAuthenticatedConnectionSharing = true;
    
    
    // Create the ColumnSet that indicates the properties to be retrieved.
    //ColumnSet cols = new ColumnSet();
    // Set the properties of the ColumnSet.
    /cols.co =new AllColumns(); // new string[] { "duplicateruleid" };
    
    
    // Create the ConditionExpression.
    ConditionExpression condition = new ConditionExpression();
    
    // Set the condition for the retrieval to be when the contact's address' city is Sammamish.
    condition.AttributeName = "baseentityname";
    condition.Operator =ConditionOperator.In;
    condition.Values =new string[] { EntityName.opportunity.ToString(), EntityName.account.ToString()  , EntityName.contact.ToString() };
    
    // Create the FilterExpression.
    FilterExpression filterRule = new FilterExpression();
    // Set the properties of the filter.
    filterRule.FilterOperator = LogicalOperator.And;
    filterRule.Conditions = new ConditionExpression[] { condition };
    
    
    // Create the QueryExpression object.
    QueryExpression queryRule = new QueryExpression();
    
    
    // Set the properties of the QueryExpression object.
    queryRule.EntityName = EntityName.duplicaterule.ToString();
    queryRule.ColumnSet = new AllColumns();
    queryRule.Criteria = filterRule;
    
    RetrieveMultipleRequest RetRules = new RetrieveMultipleRequest();
    RetRules.Query = queryRule;
    RetrieveMultipleResponse DelRules = (RetrieveMultipleResponse)incService.Execute(RetRules);
    
     //if duplicate rules found
    if (DelRules.BusinessEntityCollection.BusinessEntities.Length > 0)
     {
    //MessageBox.Show(DelRules.BusinessEntityCollection.BusinessEntities.Length.ToString());  
    
    foreach (BusinessEntity be in DelRules.BusinessEntityCollection.BusinessEntities)
    {
    duplicaterule myCrmObject = (duplicaterule)be;
    
    TargetDeleteDuplicateRule deleteRule = new TargetDeleteDuplicateRule();
    deleteRule.EntityId = myCrmObject.duplicateruleid.Value;  
    
    DeleteRequest deleteRequest = new DeleteRequest();
    deleteRequest.Target = deleteRule;
    
    DeleteResponse deleted = (DeleteResponse)incService.Execute(deleteRequest);
    }
    
    }
    Hope this Helps... 
    • Proposed as answer by Mayank Pujara Sunday, October 11, 2009 12:00 PM
    Sunday, October 11, 2009 11:59 AM