0

While trying to add multiple records on my Custom object Book using standard Controller and extension . While clicking on the submit button records are not inserted . Could you please check at my extension Logic and do the needful !! Even my User Interface is not showing the name field is required . (I mean Book Name )

Visual Force Page

<apex:page sidebar="false" standardController="Book__c" extensions="addMultipleBooksExtension">
<apex:form id="formId">
<apex:pageBlock >
<apex:pageBlockSection >
<apex:inputField value="{!Book__c.Name}" />
<apex:inputField value="{!Book__c.Title__c}"/>
<apex:inputField value="{!Book__c.Author__c}"/>
<apex:inputField value="{!Book__c.Price__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:commandButton value="Add one more Book" action="{!addBook}" />
<apex:commandButton value="Save your Records" action="{!saveBookRecords}"/>
</apex:form>
</apex:page>

Page Extension

public with sharing class addMultipleBooksExtension {

    public list<Book__c> listBook{get;set;}

    public addMultipleBooksExtension(ApexPages.StandardController controller) {
    list<Book__c> listBook= new list<Book__c>();
    }

  public void addBook(){
      Book__c book= new Book__c() ;
      listBook = new list<BooK__c>();
      listBook.add(book);
  }

  public PageReference saveBookRecords(){
      listBook = new list<Book__c>();
      Integer i ;
      for (i=0 ; i < listBook.size();i++){
      insert listBook;
      }
      return null;
  }
}

1 Answer 1

1

You're clearing the list each time you add a new book, and your save method also creates a new list, which wipes out the previous work; it causes all the input to be lost.

Here's a working version of your controller:

public class addMultipleBooksExtension {
    public list<Book__c> listBook{get;set;}

    public addMultipleBooksExtension(ApexPages.StandardController controller) {
        listBook= new list<Book__c>();
    }

  public void addBook(){
      listBook.add(new Book__c());
  }

  public void saveBookRecords(){
    upsert listBook;
  }
}
6
  • Visualforce Error Help for this Page System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!addBook}' in component <apex:commandButton> in page addmultiplebookstolibrary: Class.addMultipleBooksExtension.addBook: line 10, column 1 Class.addMultipleBooksExtension.addBook: line 10, column 1
    – Tawseef
    Commented Mar 3, 2015 at 19:02
  • Visualforce Error Help for this Page System.NullPointerException: Attempt to de-reference a null object Error is in expression '{!addBook}' in component <apex:commandButton> in page addmultiplebookstolibrary: Class.addMultipleBooksExtension.addBook: line 10, column 1 Class.addMultipleBooksExtension.addBook: line 10, column 1
    – Tawseef
    Commented Mar 3, 2015 at 19:02
  • i tried to use your code but it is showing the above mentioned error..
    – Tawseef
    Commented Mar 3, 2015 at 19:13
  • @Tawseef Sorry, I missed a variable shadowing effect. Try the new updated version.
    – sfdcfox
    Commented Mar 3, 2015 at 20:08
  • still not working ,, can u please try to run there and check ...
    – Tawseef
    Commented Mar 4, 2015 at 13:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .