Hi i am new to dynamics 365, i am trying to create a duplicate detection plugin using the dynamics 365 duplicate detection sample. can somebody helps ?
using System; using System.ServiceModel; using System.ServiceModel.Description; // These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly // located in the SDK\bin folder of the SDK download. using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Messages; using Microsoft.Crm.Sdk.Messages; namespace TestPlug { ////// Demonstrates how to do basic entity operations like create, and /// update, using Duplicate Detection attribute. /// /// At run-time, you will be given the option to delete all the /// database records created by this program. public class InvokeDuplicateDetectionForCreateAndUpdate : IPlugin { private Guid _accountId; private Guid _ruleId; private Guid _dupAccountId; private OrganizationServiceProxy _serviceProxy; private IOrganizationService _service; public void Execute(IServiceProvider serviceProvider) { ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); // Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); // Obtain the organization service reference which you will need for IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); // The InputParameters collection contains all the data passed in the message request. if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { // Obtain the target entity from the input parameters. Entity entity = (Entity)context.InputParameters["Target"]; try { CreateRequiredRecords(); // Create and account record with the named Proseware, Inc. and already existing Account Number. // Instantiate an account object. Entity account = new Entity("account"); // Set the required attributes. For account, only the name is required. account["name"] = "Proseware, Inc."; //Set any other attribute values. account["accountnumber"] = "ACC005"; // Create operation by suppressing duplicate detection CreateRequest reqCreate = new CreateRequest(); reqCreate.Target = account; reqCreate.Parameters.Add("SuppressDuplicateDetection", true); // Change to false to activate the duplicate detection. CreateResponse createResponse = (CreateResponse)_service.Execute(reqCreate); _dupAccountId = createResponse.id; Console.Write("Account: {0} {1} created with SuppressDuplicateDetection to true, ", account["name"], account["accountnumber"]); // Retrieve the account containing with its few attributes. ColumnSet cols = new ColumnSet( new String[] { "name", "accountnumber" }); var retrievedAccount =service.Retrieve("account", _dupAccountId, cols); Console.Write("retrieved, "); // Update the existing account with new account number. retrievedAccount["accountnumber"] = "ACC006"; // Update operation – update record, if a duplicate is not found. UpdateRequest reqUpdate = new UpdateRequest(); reqUpdate.Target = retrievedAccount; reqUpdate["SuppressDuplicateDetection"] = false; // Duplicate detection is activated. // Update the account record. UpdateResponse updateResponse = (UpdateResponse)_service.Execute(reqUpdate); Console.WriteLine("and updated."); DeleteRequiredRecords(promptforDelete); } catch (FaultException ex) { throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex); } catch (Exception ex) { tracingService.Trace("MyPlugin: {0}", ex.ToString()); throw; } ////// Creates any entity records that this sample requires. /// public void CreateRequiredRecords() { // Create an account record named Fourth Coffee. Entity account = new Entity("account"); // Set the required attributes. For account, only the name is required. account["name"] = "Fourth Coffee"; //Set any other attribute values. account["accountnumber"] = "ACC005"; _accountId = service.Create(account); Console.Write("Account {0} {1} created, ", account["name"], account["accountnumber"]); // Create a duplicate detection rule DuplicateRule accountDuplicateRule = new DuplicateRule { Name = "DuplicateRule: Accounts with the same Account Number", BaseEntityName = "account", MatchingEntityName = "account" }; _ruleId = service.Create(accountDuplicateRule); //Create a duplicate detection rule condition DuplicateRuleCondition accountDupCondition = new DuplicateRuleCondition { BaseAttributeName = "accountnumber", MatchingAttributeName = "accountnumber", OperatorCode = new OptionSetValue(0), // Exact Match. RegardingObjectId = new EntityReference(DuplicateRule.EntityLogicalName, _ruleId) }; Guid conditionId = service.Create(accountDupCondition); Console.Write("'{0}' created, ", accountDuplicateRule.Name); // Execute the publish request. } /// Deletes any entity records that were created for this sample. public void DeleteRequiredRecords(bool prompt) { } } } } }