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....
The Apex Code for the Visualforce Page is as below...
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
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
<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 hereStep 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
Having something look standard and uniform really matters.. You would like to present the user with the Standard look and feel wherever possible..
Situations in which this may not be possible are
Click here to read the Salesforce documentation. The following three files are the most commonly used.
Note that i have specified "na5.salesforce.com" in the URL, this is to be used when your page would be displayed outside of Salesforce otherwise you may omit this part. This may be any instance, not specifically na5... This file is public so anyone can access it. you do not need a Salesforce username and password to access it..
Scenario:
I have a table to display in my Visualforce email template. I am using a HTML table. Since the apex:detail tag is not available with Visualforce email templates, i will display the information in a HTML table and apply salesforce look and feel o make it appear as a standard Detail Page...
Plain HTML Table:
Preview:
HTML Table with Salesforce look and feel:
Preview:
Code Explanation:
The blue colored part in the code above is the modifications that we have done to our plain HTML table. Now ,let's analyze the code in detail... The Head tag as you know contain links to salesforce style sheets...
This sets the color of the Page layout to Opportunity since we have specified Opportunitytab in the styleclass.. If you set it o Accounttab the style of Account will be applied....
This div denotes the pageblock this is the main outer div....
This div denotes the pageblocksection.
This div denotes the data to be displayed inside the pabeblocksection.
Inside the HTML table we have td class="labelCol", this is used only for headings to make sure that it is bold and is right aligned..
Complete code of Visualforce Email Template:
Situations in which this may not be possible are
- Email Templates
- Web-to-Lead
- Etc etc
Click here to read the Salesforce documentation. The following three files are the most commonly used.
Note that i have specified "na5.salesforce.com" in the URL, this is to be used when your page would be displayed outside of Salesforce otherwise you may omit this part. This may be any instance, not specifically na5... This file is public so anyone can access it. you do not need a Salesforce username and password to access it..
<link class="user" href="https://na5.salesforce.com/sCSS/16.0/sprites/1245343872000/Theme2/default/elements.css" rel="stylesheet" type="text/css" />
<link class="user" href="https://na5.salesforce.com/sCSS/16.0/sprites/1245343872000/Theme2/default/common.css" rel="stylesheet" type="text/css" />
<link href="https://na5.salesforce.com/sCSS/16.0/sprites/1251310209000/Theme2/dStandard.css" rel="stylesheet" type="text/css" />
Scenario:
I have a table to display in my Visualforce email template. I am using a HTML table. Since the apex:detail tag is not available with Visualforce email templates, i will display the information in a HTML table and apply salesforce look and feel o make it appear as a standard Detail Page...
Plain HTML Table:
<table style="font-size:100%" border="0" cellpadding="5" border="0" cellspacing="0">
<tr>
<td>Opportunity Name:</td><td >{!Relatedto.Name}</td>
<td>Stage:</td><td ><apex:outputfield value="{!Relatedto.StageName}"/></td>
</tr>
<tr>
<td><apex:outputlabel value="Account:"/></td><td ><apex:outputfield value="{!Relatedto.Account.Name}"/></td>
<td><apex:outputlabel value="Close Date:"/></td><td ><apex:outputfield value="{!Relatedto.CloseDate}"/></td>
</tr>
<tr>
<td><apex:outputlabel value="Amount:"/></td><td><apex:outputField value="{!Relatedto.Amount}"/></td>
<td><apex:outputlabel value="Primary Campaign:"/></td><td ><apex:outputfield value="{!Relatedto.Campaign.Name}"/></td>
</tr>
</table>
Preview:
HTML Table with Salesforce look and feel:
<head>
<link class="user" href="https://na5.salesforce.com/sCSS/16.0/sprites/1245343872000/Theme2/default/elements.css" rel="stylesheet" type="text/css" />
<link class="user" href="https://na5.salesforce.com/sCSS/16.0/sprites/1245343872000/Theme2/default/common.css" rel="stylesheet" type="text/css" />
<link href="https://na5.salesforce.com/sCSS/16.0/sprites/1251310209000/Theme2/dStandard.css" rel="stylesheet" type="text/css" />
</head>
<body class="opportunityTab detailPage">
<div class="bPageBlock secondaryPalette">
<div class="pbBody">
<div class="pbSubsection">
<table >
<tr>
<td class="labelCol">Opportunity Name:</td><td >{!Relatedto.Name}</td>
<td class="labelCol">Stage:</td><td ><apex:outputfield value="{!Relatedto.StageName}"/></td>
</tr>
<tr>
<td class="labelCol"><apex:outputlabel value="Account:"/></td><td ><apex:outputfield value="{!Relatedto.Account.Name}"/></td>
<td class="labelCol"><apex:outputlabel value="Close Date:"/></td><td ><apex:outputfield value="{!Relatedto.CloseDate}"/></td>
</tr>
<tr>
<td class="labelCol"><apex:outputlabel value="Amount:"/></td><td><apex:outputField value="{!Relatedto.Amount}"/></td>
<td class="labelCol"><apex:outputlabel value="Primary Campaign:"/></td><td ><apex:outputfield value="{!Relatedto.Campaign.Name}"/></td>
</tr>
</table>
</div>
</div>
</div>
</body>
Preview:
Code Explanation:
The blue colored part in the code above is the modifications that we have done to our plain HTML table. Now ,let's analyze the code in detail... The Head tag as you know contain links to salesforce style sheets...
<body class="opportunityTab detailPage">
This sets the color of the Page layout to Opportunity since we have specified Opportunitytab in the styleclass.. If you set it o Accounttab the style of Account will be applied....
<div class="bPageBlock secondaryPalette">
This div denotes the pageblock this is the main outer div....
<div class="pbBody">
This div denotes the pageblocksection.
<div class="pbSubsection">
This div denotes the data to be displayed inside the pabeblocksection.
Inside the HTML table we have td class="labelCol", this is used only for headings to make sure that it is bold and is right aligned..
Complete code of Visualforce Email Template:
<messaging:emailTemplate subject="Testing Email Template with Salesforce look" recipientType="Contact" relatedToType="Opportunity">
<messaging:htmlEmailBody >
<head>
<link class="user" href="https://na5.salesforce.com/sCSS/16.0/sprites/1245343872000/Theme2/default/elements.css" rel="stylesheet" type="text/css" />
<link class="user" href="https://na5.salesforce.com/sCSS/16.0/sprites/1245343872000/Theme2/default/common.css" rel="stylesheet" type="text/css" />
<link href="https://na5.salesforce.com/sCSS/16.0/sprites/1251310209000/Theme2/dStandard.css" rel="stylesheet" type="text/css" />
</head>
<body class="opportunityTab detailPage">
<div class="bPageBlock secondaryPalette">
<div class="pbBody">
<div class="pbSubsection">
<table style="font-size:100%" border="0" cellpadding="5" border="0" cellspacing="0">
<tr>
<td class="labelCol">Opportunity Name:</td><td >{!Relatedto.Name}</td>
<td class="labelCol">Stage:</td><td ><apex:outputfield value="{!Relatedto.StageName}"/></td>
</tr>
<tr>
<td class="labelCol"><apex:outputlabel value="Account:"/></td><td ><apex:outputfield value="{!Relatedto.Account.Name}"/></td>
<td class="labelCol"><apex:outputlabel value="Close Date:"/></td><td ><apex:outputfield value="{!Relatedto.CloseDate}"/></td>
</tr>
<tr>
<td class="labelCol"><apex:outputlabel value="Amount:"/></td><td><apex:outputField value="{!Relatedto.Amount}"/></td>
<td class="labelCol"><apex:outputlabel value="Primary Campaign:"/></td><td ><apex:outputfield value="{!Relatedto.Campaign.Name}"/></td>
</tr>
</table>
</div>
</div>
</div>
</body>
</messaging:htmlEmailBody>
</messaging:emailTemplate>