function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC DummySFDC Dummy 

VF Page Error

Hi Guys

I am facing one Vf Page error.i have created two pageblock table but when i am save the data its only saved one pageblocktable field.and throwing error ,how to achieve this

error
-----------

Visualforce ErrorHelp for this Page
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Related Bank Acc]: [Related Bank Acc]
Error is in expression '{!save}' in component <apex:commandButton> in page add: Class.ManageListController1.save: line 62, column 1
Class.ManageListController1.save: line 62, column 1


 
public class ManageListController1 
{

    
    public ManageListController1(ApexPages.StandardController controller) {

    }

 public List<AccountWrapper> wrappers {get; set;}
  public List<AccountWrapper> wrappers1 {get; set;}
 public static Integer toDelIdent {get; set;}
 public static Integer addCount {get; set;}
 private Integer nextIdent=0;
  
 public ManageListController1()
 {
  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 addRows()
 {
  for (Integer idx=0; idx<addCount; idx++)
  {
   wrappers.add(new AccountWrapper(nextIdent++));
  }
 }
  
 public PageReference save()
 {
  List<BankBook__c> accs=new List<BankBook__c>();
 
  for (AccountWrapper wrap : wrappers )
  {
   accs.add(wrap.acc);
   
  }

 insert accs;
  
 
  List<BankBook__c> accs1=new List<BankBook__c>();
 
  for (AccountWrapper wrap1 : wrappers1 )
  {
   accs1.add(wrap1.acc);
   
  }

 insert accs1;
  
   
  return new PageReference('/' + Schema.getGlobalDescribe().get('BankBook__c').getDescribe().getKeyPrefix() + '/o');
 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 public pageReference parentPage () {

          return new pageReference('/apex/Receivables');
} 
  
  
  
 public class AccountWrapper
 {
  public BankBook__c acc {get; private set;}
  public Integer ident {get; private set;}
   
  public AccountWrapper(Integer inIdent)
  {
   ident=inIdent;
   acc=new BankBook__c();
  }
 }
}
 
<apex:page tabstyle="Account" showHeader="false" sidebar="false" Controller="ManageListController1">

<apex:define name="body">
            
            <div style="width:900px;margin: 10px auto 10px auto;background-color:Gray;">
             <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="Bank Book Entry:">
      
       <div style="width:200px;align=Right;">
      <apex:pageBlockTable value="{!wrappers1}" var="wrapper1" id="wtable1">
      <apex:column headerValue="Date" style="background:gray;"  >
    <apex:inputField value="{!wrapper1.acc.Txn_Date__c}"/>
</apex:column>
 <apex:column headerValue="Payment Type" style="background:gray;">
    <apex:inputField value="{!wrapper1.acc.Credit_Debit__c}"/>
</apex:column>
 <apex:column headerValue="Company Account" style="background:gray;">
    <apex:inputField value="{!wrapper1.acc.Related_Bank_Acc__c}"/>
</apex:column>

         
      </apex:pageBlockTable>
      
          </div>
   <div style="width:500px;margin: 10px auto 10px auto;">
   
      <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
      
         
       
         <apex:column headerValue="Ident" style="background:gray;">
            <apex:outputText value="{!wrapper.ident}"/>
         </apex:column>
         <apex:column headerValue="Master Code" style="background:gray;">
            <apex:inputField value="{!wrapper.acc.Entries_Code__c}"/>
         </apex:column>
         <apex:column headerValue="Amount" style="background:gray;">
            <apex:inputField value="{!wrapper.acc.Amount__c}"/>
         </apex:column>
        
         <apex:column headerValue="Narration" style="background:gray;">
            <apex:inputField value="{!wrapper.acc.Narration__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:column headerValue="Action">
            <apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable" style="background:Red">
               <apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/> 
            </apex:commandButton>
         </apex:column>
         
      </apex:pageBlockTable>
     </div>
        
        
      <apex:commandButton value="Add 5 Rows" action="{!addRows}" rerender="wtable" style="width:100px;margin: 10px auto 10px auto;">
         <apex:param name="addCount" value="5" assignTo="{!addCount}"/> 
      </apex:commandButton>
      
      <apex:commandButton value="Save" action="{!save}" style="width:100px;margin: 10px auto 10px auto;"/>
   
    
     </apex:pageBlock>
 </apex:form>
  </div>
        </apex:define>
</apex:page>


 
Subhrajyoti NathSubhrajyoti Nath
Put it into try catch block
SFDC DummySFDC Dummy
already i have tried this,but unable to solve .....
SFDC DummySFDC Dummy
Is it correct..... public PageReference save() try{ List accs=new List(); for (AccountWrapper wrap : wrappers ) { accs.add(wrap.acc); } insert accs; catch(Exception e) { List accs1=new List(); for (AccountWrapper wrap1 : wrappers1 ) { accs1.add(wrap1.acc); } insert accs1; return new PageReference('/' + Schema.getGlobalDescribe().get('BankBook__c').getDescribe().getKeyPrefix() + '/o'); }
Subhrajyoti NathSubhrajyoti Nath
Try like this, see if that works or not



try{
for (AccountWrapper wrap : wrappers )
  {
   accs.add(wrap.acc);
   }
insert accs;

catch(DmlException e) {
               System.debug('The following exception has occurred: ' + e.getMessage());
}
SFDC DummySFDC Dummy
I have tried this but record not saved in custom object public PageReference save() { List accs=new List(); try{ for (AccountWrapper wrap : wrappers ) { accs.add(wrap.acc); } insert accs; } catch(DmlException e) { System.debug('The following exception has occurred: ' + e.getMessage()); } return new PageReference('/' + Schema.getGlobalDescribe().get('BankBook__c').getDescribe().getKeyPrefix() + '/o');
developer_sfdeveloper_sf
You will need to enter required fields of object in order to save record.