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 

System.DmlException

Hi All

I am facing problem saving record from two pageblock table in a single vf page

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

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_close}' in component <apex:commandButton> in page newbankbook: Class.addrowCon.save_close: line 22, column 1
Class.addrowCon.save_close: line 22, column 1

 
public class addrowCon
{ 
  public integer num { get; set; }
  public List<BankBook__c > accts {get; set;}
  public List<BankBook__c > accts1 {get; set;} 
  public addrowCon()
  {
     accts = new List<BankBook__c >(); 
     accts.add(new BankBook__c ()); 
     accts1 = new List<BankBook__c >(); 
     accts1.add(new BankBook__c ());    
  }
   public PageReference add_rows() {
    for(integer i=0;i<num ;i++)
        {
          accts.add(new BankBook__c ());    
        }
        
        return null;
     }
  public PageReference save_close()
  {       insert accts;
          insert accts1;
     PageReference home = new PageReference('/home/home.jsp');
     home.setRedirect(true);
     return home;
  }
}
 
<apex:page controller="addrowCon" >
<apex:form >
<apex:sectionHeader title="flexandsalesforceblog" subtitle="AddRows" help="Http://flexandsalesforce.blogspot.com"/>
   <apex:pageBlock title="Add row Dynamically" >
     <apex:pageBlockButtons location="bottom">
         <apex:commandButton value="Save" action="{!save_close}" rerender="error"/>
       </apex:pageBlockButtons>
       <Div align="right">
             <apex:inputText value="{!num}" style="width:45px"/>
             <apex:commandButton value="Add rows" action="{!add_rows}"/> 
       </Div>
       <br/>
       <apex:pageBlockTable value="{!accts1}" var="a1" id="table1">
          
          <apex:column headerValue="Date" style="background:gray;"  >
    <apex:inputField value="{!a1.Txn_Date__c}"/>
</apex:column>
 <apex:column headerValue="Payment Type" style="background:gray;">
    <apex:inputField value="{!a1.Credit_Debit__c}"/>
</apex:column>
 <apex:column headerValue="Company Account" style="background:gray;">
    <apex:inputField value="{!a1.Related_Bank_Acc__c}"/>
</apex:column>
      
       </apex:pageBlockTable>
       
       
      <apex:pageBlockTable value="{!accts}" var="a" id="table">
         <apex:column headerValue="Master Code" style="background:gray;">
            <apex:inputField value="{!a.Entries_Code__c}"/>
         </apex:column>
         <apex:column headerValue="Amount" style="background:gray;">
            <apex:inputField value="{!a.Amount__c}"/>
         </apex:column>
        
         <apex:column headerValue="Narration" style="background:gray;">
            <apex:inputField value="{!a.Narration__c}"/>
         </apex:column>
       </apex:pageBlockTable>
   </apex:pageBlock>
    </apex:form>
</apex:page>

 
Sagar PareekSagar Pareek
In the second page block table you are not asking the user to enter any value. The field is required field on the object level. Either make the field non required or add the field in your second page block table. This will resolve the issue.
 
SFDC DummySFDC Dummy
i need to save both block there is required field in both block and i am entering the value but its showing same error
atla satheeshkumaratla satheeshkumar
you are inserting 2 lists separately.that's why it is causing the issue.
accts is inserting first so acct1 is giving Error.

 
SFDC DummySFDC Dummy
how i will insert 2 list at a time
atla satheeshkumaratla satheeshkumar
Create new List and add existing lists to that and insert it.you will not get Errorr.

List<BankBook__c> combinedList = New List<BankBook__c>();
combinedList.Addall(accts1);
combinedList.Addall(accts);

public PageReference save_close()

  {       

          insert combinedList;

      PageReference home = new PageReference('/home/home.jsp');

      home.setRedirect(true);

      return home;

   }
SFDC DummySFDC Dummy

public class addrowCon
{ 
  public integer num { get; set; }
  public List<BankBook__c > combinedList {get; set;}
  public List<BankBook__c > accts {get; set;}
  public List<BankBook__c > accts1 {get; set;} 
  public static Integer addCount {get; set;}
  private Integer nextIdent=1;
  public addrowCon()
  {
     
     
  List<BankBook__c>  combinedList = New List<BankBook__c>();
    combinedList.Addall(accts1);
    combinedList.Addall(accts );   
  }
   
     
     
  public PageReference save_close()
  {       insert combinedList; 
        
     PageReference home = new PageReference('/home/home.jsp');
     home.setRedirect(true);
     return home;
  }
}
I have tried this but throwing error System.NullPointerException: Attempt to de-reference a null object 
Class.addrowCon.<init>: line 14, column 1
SFDC DummySFDC Dummy
Its not fetching value field.....
atla satheeshkumaratla satheeshkumar
Hi,
Please use below code I have Executed in my org. it is working fine.
Make sure before inserting give all mandatory fileds. otherwise you wil get Same Error.

VF Code:
<apex:page controller="addrowCon" >
<apex:form >
<apex:sectionHeader title="flexandsalesforceblog" subtitle="AddRows" help="Http://flexandsalesforce.blogspot.com"/>
   <apex:pageBlock title="Add row Dynamically" >
     <apex:pageBlockButtons location="bottom">
         <apex:commandButton value="Save" action="{!save_close}"/>
       </apex:pageBlockButtons>
       <Div align="right">
             <apex:inputText value="{!num}" style="width:45px"/>
             <apex:commandButton value="Add rows" action="{!add_rows}"/> 
       </Div>
       <br/>
       <apex:pageBlockTable value="{!accts1}" var="a1" id="table1">
          
          <apex:column headerValue="OpportunityName" style="background:gray;"  >
    <apex:inputField value="{!a1.name}"/>
</apex:column>
 <apex:column headerValue="Stage Name" style="background:gray;">
    <apex:inputField value="{!a1.StageName}"/>
</apex:column>
 <apex:column headerValue="Date" style="background:gray;">
    <apex:inputField value="{!a1.closeDate}"/>
</apex:column>
      
       </apex:pageBlockTable>
       
       
      <apex:pageBlockTable value="{!accts}" var="a" id="table">
         <apex:column headerValue="Opportunity name" style="background:gray;">
            <apex:inputField value="{!a.name}"/>
         </apex:column>
         <apex:column headerValue="Probability" style="background:gray;">
            <apex:inputField value="{!a.StageName}"/>
         </apex:column>
        
         <apex:column headerValue="Dtae" style="background:gray;">
            <apex:inputField value="{!a.closeDate}"/>
         </apex:column>
       </apex:pageBlockTable>
   </apex:pageBlock>
    </apex:form>
</apex:page>


Controller:
public class addrowCon

  public integer num { get; set; }
  public List<opportunity > accts {get; set;}
  public List<opportunity > accts1 {get; set;} 
  public  List<Opportunity>  combinedList = New List<Opportunity>();

  public addrowCon()
  {
     accts = new List<opportunity >(); 
     accts.add(new opportunity ()); 
     accts1 = new List<opportunity >(); 
     accts1.add(new opportunity ());    
  }
   public Pagereference add_rows() {
    for(Integer i=0;i<num;i++){
    accts.add(new Opportunity());
    }
    return null;    
     }
     
  public PageReference save_close()
  {  
       
  combinedList.Addall(accts1);
  combinedList.Addall(accts );  
  insert combinedList;

     PageReference home = new PageReference('/home/home.jsp');
     home.setRedirect(true);
     return home;
  }
}

Regards
Satish Atla

 
atla satheeshkumaratla satheeshkumar
I have used "Opportunity" instead of "BankBook__C",replace the object name as well as field names.
SFDC DummySFDC Dummy
Its t throwing error System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!save_close}' in component in page newbankbook: Class.addrowCon.save_close: line 40, column 1 Class.addrowCon.save_close: line 40, column 1
atla satheeshkumaratla satheeshkumar
It is executing for me..i have tested again. we don't have line no:40 in apex class how you are getting Error I am not understanding.Please execute my code which i have given to you.