Duplicates in Salesforce - A better approach

6:05 AM

Duplicate data is always a problem, particularly when the quantity of data is too large and when the quality of data is very critical..

Salesforce uses a 'UNIQUE' definition on a field to avoid duplicates. When this attribute is set to TRUE you will have only unique values in the field.

Problem:
Salesforce displays an error message when you try to enter a duplicate value and click on "SAVE". But, you would not want the user to enter all values and get an error message as the last step. Moreover, in some cases you would not want the user to create a new record if it already exists...

Solution:
Clearly, a search for existing records before creating a new one would be a typical solution. But not all fields are searchable.....

A more user friendly approach would be to allow the user to enter the UNIQUE field first, if the value already exists he would be shown an error message else he will be redirected to the normal New Page screen...

Step 1:

Create a Visualforce Page with the code below....

<apex:page standardController="Case" extensions="caseApex">
<apex:pageBlock title="Tickets">
<apex:form >
Enter your Ticket Number Here
&nbsp;&nbsp;<apex:inputField value="{!NewCase.Ticket_Number__c}"/>
<apex:commandButton action="{!Next}" value="click here to proceed" status="status"/>

<!-- Display error message -->
<apex:pagemessage strength="2" title="Error!!" severity="error" detail="This Ticket Already Exists !!!" rendered="{!errormsg}"/><!-- End of error message -->
<apex:pageblocktable rendered="{!NOT(ISNULL(existingcase))}" value="{!existingcase}" var="case">
<apex:column headervalue="Select">
<apex:commandlink action="{!SelectTicket}">
<input type="radio" name="CaseSel"/>
</apex:commandlink>
</apex:column>
<apex:column headervalue="Ticket Number"> <apex:outputtext value="{!case.Ticket_Number__c}"/> </apex:column>
<apex:column headervalue="Description"> <apex:outputtext value="{!case.Description}"/> </apex:column>
</apex:pageblocktable>
</apex:form>
</apex:pageBlock>
</apex:page>

The Apex Code for the Visualforce Page is as below...

public class caseApex {

public boolean errormsg=false;
public String CaseId=System.currentPageReference().getParameters().get('id');
public String tnum;
public List<Case> chkdup;
public Case newcase= new Case();

public caseApex(ApexPages.StandardController controller)
{
}
public case getnewcase()
{
return newcase;
}
public case getexistingcase()
{
if (chkdup!= NULL)
{
if(chkdup.size()>0)
return chkdup[0];
else
return null;
}
else
return null;
}

public pagereference Next()
{
chkdup=[select Ticket_Number__c,id,Description from Case where Ticket_Number__c=:newcase.Ticket_Number__c limit 1];
if(chkdup.size()==0)
{
String tnum=newcase.Ticket_Number__c;
Pagereference newcase1 = new Pagereference('/500/e?nooverride=true&00N90000000LSLd='+tnum+'');
return newcase1;
}
else
{
errormsg=true;
return null;
}

}

public pagereference SelectTicket()
{
Pagereference chkdup = new Pagereference('/'+chkdup[0].Id);
return chkdup;
}

public boolean geterrormsg()
{
return errormsg;
}
}
Please note the coloured part in the above code... This is a hardcoded value for my instance. It denotes the field Id.. A better explanation about this is available here

Step 2:

Override the "New" button of "Cases" with the Visualforce Page you created....

Screenshots:

When the user clicks on "New" button under Case he will be shown this Page




Below is a screnshot showing a message that the case already exits

3 comments

  1. Hi edwin your post is great, but i think it is not saving data in ticket_number__c field if there are no duplicates, as a result it is not showing error message even if i am entering same ticket number.

    ReplyDelete
  2. A slight change in this line will make this code work perfectly. Pagereference newcase1 = new Pagereference('/500/e?nooverride=true&00N90000000LSLd='+tnum);

    ReplyDelete
  3. Please suggest me how fetch duplicate records if more than one.In the above code it is fetching only one record
    Please help me?

    ReplyDelete