+ Start a Discussion
Money Care 7Money Care 7 

Attention error messsage

Hi All
I have created a page for entering multiple record at a time.if i am entering duplicate master code then its showing error message like this

User-added image
 
public class MasterTestEntryPage1
{
    public String message = System.CurrentPagereference().getParameters().get('msg');
    public List<AccountWrapper> wrappers {get; set;}
    public List<AccountWrapper> wrappers1 {get; set;}
    public List<MasterTest__c> master {get; set;}
    public List<BankBookTest__c> bankbook {get; set;}    
    public static Integer toDelIdent {get; set;}
    public static Integer addCount {get; set;}
    private Integer nextIdent=1;    
    
    public MasterTestEntryPage1(){
        wrappers=new List<AccountWrapper>();
        Wrappers1=new List<AccountWrapper>();
        for (Integer idx=0; idx<1; idx++){
            wrappers.add(new AccountWrapper(nextIdent++));
            wrappers1.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 delWrapper()
{
    Integer toDelPos = -1;
    for( Integer idx = 0; idx < wrappers.size(); idx++ )
    {
        if( wrappers[idx].ident == toDelIdent )
        {
            toDelPos = idx;
        }
    }       

    if( toDelPos != -1 )
    {
        MasterTest__c bk = wrappers.get( toDelPos ).acc;
        wrappers.remove( toDelPos );
        
        delete bk;
    }
} */
    

    public void addRows(){
    
    List<MasterTest__c> accs=new List<MasterTest__c>();
        for (AccountWrapper wrap : wrappers){
            accs.add(wrap.acc);
        }        
        
    
        AccountWrapper objAccWrapper;
        for (Integer idx=0; idx<addCount; idx++){
            objAccWrapper = new AccountWrapper(nextIdent++);
            if(!wrappers.isEmpty()){
                objAccWrapper.acc.Date__c= wrappers[0].acc.Date__c;
                
                
            }
            wrappers.add(objAccWrapper);
        }
    }

 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,' The code is duplicate or invalid. '));
       } 
 

        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();
        }
    }
}
<apex:page controller="MasterTestEntryPage1" tabstyle="Account" showHeader="true" sidebar="false">

<apex:pageMessages id="showmsg" ></apex:pageMessages>
<apex:define name="body">


<script type="text/javascript">
function displaymessage()
{
alert("Remember to Delete blank row that you have made!");
}
</script>
            
            <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:pageBlockSection columns="1"  ><h1><marquee>PLEASE ENTER DATE BEFORE CREATING MASTERS !!</marquee></h1>
       <apex:pageBlockTable value="{!wrappers}" var="wrapper"  style="width:70px;" >
                    <apex:column headerValue="Opening Balances as on"   >
                            <apex:inputField value="{!wrapper.acc.Date__c}"  required="true" />
                      </apex:column>
     </apex:pageBlockTable>
    
      <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:selectList multiselect="false" size="1" value="{!wrapper.acc.Type__c}" onchange="showButton()">             
                <apex:selectOption itemvalue="Party Account" itemLabel="Party Account"/>
                <apex:selectOption itemvalue="Bank Account" itemLabel="Bank Account"/>                                
            </apex:selectList>
         </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_Code__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:pageBlockSection>
      <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>
        
        <center><br/>
        <b><i style="font-size:10px;color:red;">Developed By GlobalNest IT Solution Pvt.Ltd. <a href="http://www.globalnest.com" target="_blank">[http://www.Globalnest.com]</a> </i></b>
    </center><br/>
</apex:page>
User-added image

 
GauravGargGauravGarg

Hi Money,

Master_code__c field is of unique type, hence you are not able to enter duplicate values in master code field.

To resolve this issue, you need to uncheck unique checkbox from master code fields. 
Please follow below steps:
1. Click on setup and enter Account.
2. Search for Master code field and hit edit.
3. Uncheck "Unique" checkbox and hit save. 
4. Try to enter duplicate values, this time the records will save. 

Let me know if you still have any more question on this, please mark it as best answer if this solve you problem. 

Thanks,

Gaurav
Email: gauravgarg.nmims@gmail.com
skype: gaurav62990

Money Care 7Money Care 7
Hi Gaurav here master_code is my standard unique field.so how to make unique standard name field
GauravGargGauravGarg

Hi Money,

Incase of standard unique field, we cannot enter duplicate values in Master_code field. One option is to use create one custom field and enter values in custom field as required. 

Thanks,

Gaurav

GauravGargGauravGarg
Hi Money,

Please let me know if above answer did solve your problem ?