Sunday, February 21, 2010

checking duplicate detection using custom web service in ms crm 4.0

Here is the web method for duplicate detection web service:


[WebMethod]
public string CheckDuplicateAccount(string objectguid, string name)
{
CrmService service = new CrmService();
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
if(objectguid == null || objectguid == string.Empty )
{
//Code for checking account records at creation time
try
{
QueryExpression query = new QueryExpression();
query.EntityName = EntityName.account.ToString();// Create a set of columns to return.
ColumnSet cols = new ColumnSet();
cols.Attributes = new string [] {“name”};// Create the ConditionExpression.
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = “name”;
condition.Values = new string[] {name};// Builds the filter based on the condition
FilterExpression filter = new FilterExpression();
filter.FilterOperator = LogicalOperator.And;
filter.Conditions = new ConditionExpression[] {condition};
query.ColumnSet = cols;
query.Criteria = filter;// Retrieve the values from Microsoft CRM.
BusinessEntityCollection retrieved = service.RetrieveMultiple(query);
if(retrieved.BusinessEntities.Length > 0 )
{
return “Duplicate record exists for : ” + name;
}else
{
return “”;
}
}
catch(System.Web.Services.Protocols.SoapException se)
{
return “ERROR: ” + se.Detail.InnerText;
}
}
else {//Code for checking account records at updation time
try{
QueryExpression query = new QueryExpression();
query.EntityName = EntityName.account.ToString();// Create a set of columns to return.
ColumnSet cols = new ColumnSet();
cols.Attributes = new string [] {“name”};// Create the ConditionExpression.
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = “name”;
condition1.Values = new string[] {name};
condition1.Operator = ConditionOperator.Equal;
ConditionExpression condition2 = new ConditionExpression();
condition2.AttributeName = “accountid”;
condition2.Values = new string[] {objectguid};
condition2.Operator = ConditionOperator.NotEqual;// Builds the filter based on the condition
FilterExpression filter = new FilterExpression();
filter.FilterOperator = LogicalOperator.And;
filter.Conditions = new ConditionExpression[] {condition1, ondition2};
query.ColumnSet = cols;
query.Criteria = filter;// Retrieve the values from Microsoft CRM.
BusinessEntityCollection retrieved = service.RetrieveMultiple(query);
if(retrieved.BusinessEntities.Length > 0 )
{
return “Duplicate record exists for : ” + name;
}
else
{
return “”;
}
}catch(System.Web.Services.Protocols.SoapException se)
{
return “ERROR: ” + se.Detail.InnerText;
}
}


call this web method using java script .

1 comment:

  1. Hi Guru,

    Am looking for duplicate detection for combination of two entities on subgrid.
    For eg: Issue type: A, B, C, D
    Sub Issue Type: E, F, G, H
    Combination A-E
    if user enter A-E combination again. It should throw an error that the value is duplicate. Can I use above implementation for my process?

    ReplyDelete