Tuesday, 25 February 2014

Microsoft CRM Create Request Using Duplicate Detection Rules

The Following is an example on how to fire Duplicate Detection Rules when using the CreateRequest/UpdatieRequest Records via

SuppressDuplicateDetection
The Key Property here is "SuppressDuplicateDetection"
If you set "SuppressDuplicateDetection" to False duplicate detection will run. If you set it to True an exception with the below message will be thrown if a Duplicate is found

"A record was not created or updated because a duplicate of the current record already exists"

False Is the Default

Example


 //AccountAccount account = new Account
{
Name = "Proseware, Inc.",
AccountNumber = "ACC005"
};
//Here is an Example on How to create a record wth duplicate detection turned off. // Create operation by suppressing duplicate detectionCreateRequest reqCreate = new CreateRequest();
reqCreate.Target = account;
reqCreate.Parameters.Add("SuppressDuplicateDetection", true);
CreateResponse createResponse = (CreateResponse)_service.Execute(reqCreate);
Guid _dupAccountId = createResponse.id;
 //Account will create because duplicate detection is truned off 

//Here is an Example on How to create a record wth duplicate detection turned ON. // Create operation by suppressing duplicate detectionCreateRequest reqCreate = new CreateRequest();
reqCreate.Target = account;
reqCreate.Parameters.Add("SuppressDuplicateDetection", false);
try{CreateResponse createResponse = (CreateResponse)_service.Execute(reqCreate);
_dupAccountId = createResponse.id;
 //Account will create if no duplicate }
catch (FaultException<OrganizationServiceFault> ex){
if (ex.Detail.ErrorCode == -2147220685){
// duplicate detected: Handle it here}
else{
throw;
}




The same can be done via the UpdateRequest when updating a record 

No comments:

Post a Comment