+ Start a Discussion
SFDC DummySFDC Dummy 

Error message display when duplicate record insert in vf page

Hi All

I have created vf page for inserting record .i need to put condtion if i am entering any duplicate recordit will be display error message..

like i have field
Type
master code==unique
mastername
opening balnce

if i will be entered any duplicate mastercode it will display error message
 
<apex:page controller="MasterTestEntryPage" tabstyle="Account" showHeader="true" sidebar="false">

<apex:pageMessages id="showmsg" ></apex:pageMessages>
<apex:define name="body">
            
            <div style="width:1000px;margin: 0px auto 10px auto;background-color:Gray;border: 1px solid black;">
             <apex:image id="theImage" value="{!$Resource.Emami}" width="100" height="100"/>
             <apex:outputLink style="font-weight: bold;float:right;" value="{!URLFOR($Page.LoginPage)}"><b>Back To Home</b></apex:outputLink>
 <apex:form >
   <apex:pageBlock title="Master Entry:">
   
    
      <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
      
      <apex:column headerValue="">
            <apex:commandButton value="X" action="{!delWrapper}" rerender="wtable" style="background:red">
               <apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/> 
            </apex:commandButton>
         </apex:column>
         <apex:column headerValue="Sl.No" style="background:pink;">
            <apex:outputText value="{!wrapper.ident}"/>
         </apex:column>
         <apex:column headerValue="Select Account" style="background:pink;">
            <apex:inputField value="{!wrapper.acc.Type__c}" />
         </apex:column>
         <apex:column headerValue="Master Code" style="background:pink;">
            <apex:inputField value="{!wrapper.acc.Name}" />
         </apex:column>
         <apex:column headerValue="Master Name" style="background:pink;">
            <apex:inputField value="{!wrapper.acc.Master_Name__c}"/>
         </apex:column>
         <apex:column headerValue="Opening Balance" style="background:pink;">
         
                        <apex:inputField value="{!wrapper.acc.Opening_Balance__c}"/>
                   
            
         </apex:column>
         
         <apex:column headerValue="Remarks" style="background:pink;">
            <apex:inputField value="{!wrapper.acc.Remarks__c}"/>
         </apex:column>
       
        
       
       <apex:column headerValue="">
            <apex:commandButton value="Enter" action="{!addRows}" rerender="wtable" style="background:pink" >
         <apex:param name="addCount" value="1" assignTo="{!addCount}"/> 
      </apex:commandButton>
         </apex:column>
       
        
      </apex:pageBlockTable>
      
      <apex:commandButton value="Add 5 Rows" action="{!addRows}" rerender="wtable">
         <apex:param name="addCount" value="5" assignTo="{!addCount}"/> 
      </apex:commandButton>
       <apex:commandButton value="View Master List" action="{!parentPage }" rerender="wtable">
          
      </apex:commandButton>
      <apex:commandButton value="Save Master" action="{!save}"  />
   </apex:pageBlock>
 </apex:form>
  </div>
        </apex:define>
        
       
</apex:page>
--------------------------------------------------------------------------
/*---------------------------------------------------------------------
Version      : 34.0
Company      : Globalnest It Solution Pvt. Ltd.
Author       : Maheswar P
Date         : 26/07/2015
Description  : controller for VF page "MasterTestEntryPage "
History      :             
-------------------------------------------------------------------------*/


public class MasterTestEntryPage 
{
 public List<AccountWrapper> wrappers {get; set;}
 public static Integer toDelIdent {get; set;}
 public static Integer addCount {get; set;}
 private Integer nextIdent=1;
  
 public MasterTestEntryPage()
 {
  wrappers=new List<AccountWrapper>();
  for (Integer idx=0; idx<1; idx++)
  {
   wrappers.add(new AccountWrapper(nextIdent++));
  }
 }
  
 public void delWrapper()
 {
  Integer toDelPos=-1;
  for (Integer idx=0; idx<wrappers.size(); idx++)
  {
   if (wrappers[idx].ident==toDelIdent)
   {
    toDelPos=idx;
   }
  }
   
  if (-1!=toDelPos)
  {
   wrappers.remove(toDelPos);
  }
 }
  
 public void addRows()
 {
  for (Integer idx=0; idx<addCount; idx++)
  {
   wrappers.add(new AccountWrapper(nextIdent++));
  }
 }
  
 public PageReference save(){
  //try{
  List<MasterTest__c> accs=new List<MasterTest__c>();
  for (AccountWrapper wrap : wrappers)
  {
   accs.add(wrap.acc);
   
  }
   insert accs;
  
  return new PageReference('/' + Schema.getGlobalDescribe().get('MasterTest__c').getDescribe().getKeyPrefix() + '/o');
  
  //}
  
  //catch(Exception e)
        //{  
        //ApexPages.Message myMsg = new  ApexPages.Message(ApexPages.Severity.ERROR,'Sorry...... You have entered DUPLICATE MASTER CODE' );
           // ApexPages.addMessage(myMsg); 
         
           //Apexpages.addMessage(new Apexpages.message(ApexPages.Severity.Error,' Record already exist with same Master Code'));
      // } 
 
        return null; 
  
  
  
  
 }
 
 public pageReference parentPage () {

          return new pageReference('/apex/MasterDetailsPage');
} 
  
  
  
 public class AccountWrapper
 {
  public MasterTest__c acc {get; private set;}
  public Integer ident {get; private set;}
   
  public AccountWrapper(Integer inIdent)
  {
   ident=inIdent;
   acc=new MasterTest__c();
  }
 }
}

 
salesforce mesalesforce me
Hi check this one...
<apex:page controller="MasterTestEntryPage" tabstyle="Account" showHeader="true" sidebar="false">

<apex:pageMessages id="showmsg" ></apex:pageMessages>
<apex:define name="body">
            
            <div style="width:1000px;margin: 0px auto 10px auto;background-color:Gray;border: 1px solid black;">
             <apex:image id="theImage" value="{!$Resource.Emami}" width="100" height="100"/>
             <apex:outputLink style="font-weight: bold;float:right;" value="{!URLFOR($Page.LoginPage)}"><b>Back To Home</b></apex:outputLink>
 <apex:form >
   <apex:pageBlock title="Master Entry:">
   
    
      <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
      
      <apex:column headerValue="">
            <apex:commandButton value="X" action="{!delWrapper}" rerender="wtable" style="background:red">
               <apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/> 
            </apex:commandButton>
         </apex:column>
         <apex:column headerValue="Sl.No" style="background:pink;">
            <apex:outputText value="{!wrapper.ident}"/>
         </apex:column>
         <apex:column headerValue="Select Account" style="background:pink;">
            <apex:inputField value="{!wrapper.acc.AccountNumber}" />
         </apex:column>
         <apex:column headerValue="Master Code" style="background:pink;">
            <apex:inputField value="{!wrapper.acc.Name}" />
         </apex:column>
         <apex:column headerValue="Master Name" style="background:pink;">
            <apex:inputField value="{!wrapper.acc.Phone}"/>
         </apex:column>
         <apex:column headerValue="Opening Balance" style="background:pink;">
         
                        <apex:inputField value="{!wrapper.acc.Site}"/>
                   
            
         </apex:column>
         
         <apex:column headerValue="Remarks" style="background:pink;">
            <apex:inputField value="{!wrapper.acc.Industry}"/>
         </apex:column>
       
        
       
       <apex:column headerValue="">
            <apex:commandButton value="Enter" action="{!addRows}" rerender="wtable" style="background:pink" >
         <apex:param name="addCount" value="1" assignTo="{!addCount}"/> 
      </apex:commandButton>
         </apex:column>
       
        
      </apex:pageBlockTable>
      
      <apex:commandButton value="Add 5 Rows" action="{!addRows}" rerender="wtable">
         <apex:param name="addCount" value="5" assignTo="{!addCount}"/> 
      </apex:commandButton>
       <apex:commandButton value="View Master List" action="{!parentPage }" rerender="wtable">
          
      </apex:commandButton>
      <apex:commandButton value="Save Master" action="{!save}"  />
   </apex:pageBlock>
 </apex:form>
  </div>
        </apex:define>
        
       
</apex:page>

  Apex:--
 
public class MasterTestEntryPage 
{
 public List<AccountWrapper> wrappers {get; set;}
 public static Integer toDelIdent {get; set;}
 public static Integer addCount {get; set;}
 private Integer nextIdent=1;
  
 public MasterTestEntryPage()
 {
  wrappers=new List<AccountWrapper>();
  for (Integer idx=0; idx<1; idx++)
  {
   wrappers.add(new AccountWrapper(nextIdent++));
  }
 }
  
 public void delWrapper()
 {
  Integer toDelPos=-1;
  for (Integer idx=0; idx<wrappers.size(); idx++)
  {
   if (wrappers[idx].ident==toDelIdent)
   {
    toDelPos=idx;
   }
  }
   
  if (-1!=toDelPos)
  {
   wrappers.remove(toDelPos);
  }
 }
  
 public void addRows()
 {
  for (Integer idx=0; idx<addCount; idx++)
  {
   wrappers.add(new AccountWrapper(nextIdent++));
  }
 }
  
 public PageReference save(){
  //try{
  List<Account> accs=new List<Account>();
  for (AccountWrapper wrap : wrappers)
  {
   accs.add(wrap.acc);
   
  }
   insert accs;
  
 // return new PageReference('/' + Schema.getGlobalDescribe().get('Account').getDescribe().getKeyPrefix() + '/o');
  return null;
  //}
  
  //catch(Exception e)
        //{  
        //ApexPages.Message myMsg = new  ApexPages.Message(ApexPages.Severity.ERROR,'Sorry...... You have entered DUPLICATE MASTER CODE' );
           // ApexPages.addMessage(myMsg); 
         
           //Apexpages.addMessage(new Apexpages.message(ApexPages.Severity.Error,' Record already exist with same Master Code'));
      // } 
 
        return null; 
  
  
  
  
 }
 
 public pageReference parentPage () {

         // return new pageReference('/apex/MasterDetailsPage');
         return null;
} 
  
  
  
 public class AccountWrapper
 {
  public Account acc {get; private set;}
  public Integer ident {get; private set;}
   
  public AccountWrapper(Integer inIdent)
  {
   ident=inIdent;
   acc=new Account();
  }
 }
}

u have any doughts ping me...