+ Start a Discussion
aditya naiduaditya naidu 

Can we fetch multiple records using controller class?

I am tring to fetch multiple record and update them, but when ever i try to fetch the second record automatically the page get reefreshed and goes to the first row.
The code is:
visualforce page:
<apex:page controller="binsert">
<apex:form >
<apex:pageBlock title="bulk insert">
<apex:pageBlockButtons >
<apex:commandButton value="add row" action="{!addr}"/>
<apex:commandButton value="save row" action="{!saver}"/>
<apex:commandButton value="fetch" action="{!fetch}"/>
<apex:commandButton value="update" action="{!updat}"/>
</apex:pageBlockButtons>
<apex:pageBlockTable var="bu" value="{!buk}">
<apex:column headerValue="Book Name">
<apex:inputField value="{!bu.name}"/>
</apex:column>
<apex:column headerValue="Book Author">
<apex:inputField value="{!bu.book_author__c}"/>
</apex:column>
<apex:column headerValue="Book Cost">
<apex:inputField value="{!bu.book_cost__c}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Controller class:
public class binsert {

    public PageReference updat() {
    update buk;
        return null;
    }


    public pagereference fetch() {
    buk=[select id,name,book_author__c,book_cost__c from book__c where name=:bo.name];
      return null;
    }


    public PageReference saver() {
    insert buk;
        return page.bulkinsert;
    }


    public PageReference addr() {
    book__c buuk=new book__c();
    buk.add(buuk);
            return null;
          
    }

book__c bo=new book__c();
public list<book__c> buk{get; set;}
public binsert(){
buk=new list<book__c>();
buk.add(bo);
}
}

can you please help me out to fetch multiple record and update.
Your suggestion is well appreciated.
Thank you!!
 
JeffreyStevensJeffreyStevens
do you have multiple Book__c records with the name bo.Name?  Your SOQL is only retrieving records  where name=:bo.name
Mahesh DMahesh D
Hi 

Please take the below code:
 
<apex:page controller="binsert">
	<apex:form >
		<apex:pageBlock title="bulk insert" id="pb">
			<apex:pageBlockButtons >
				<apex:commandButton value="add row" action="{!addr}" reRender="pb"/>
				<apex:commandButton value="save row" action="{!saver}"/>
				<apex:commandButton value="fetch" action="{!fetch}" reRender="pb"/>
				<apex:commandButton value="update" action="{!updat}" reRender="pb"/>
			</apex:pageBlockButtons>
			<apex:pageBlockTable var="bu" value="{!buk}">
				<apex:column headerValue="Book Name">
					<apex:inputField value="{!bu.name}"/>
				</apex:column>
				<apex:column headerValue="Book Author">
					<apex:inputField value="{!bu.book_author__c}"/>
				</apex:column>
				<apex:column headerValue="Book Cost">
					<apex:inputField value="{!bu.book_cost__c}"/>
				</apex:column>
			</apex:pageBlockTable>
		</apex:pageBlock>
	</apex:form>
</apex:page>

Class:
 
public class binsert {

    book__c bo=new book__c();
    public list<book__c> buk {get; set;}
    
    public binsert(){
        buk=new list<book__c>();
        buk.add(bo);
    }

    public PageReference updat() {
        update buk;
        return null;
    }

    public pagereference fetch() {
        buk=[select id,name,book_author__c,book_cost__c from book__c where name=:bo.name];
        return null;
    }

    public PageReference saver() {
        insert buk;
        return page.bulkinsert;
    }

    public PageReference addr() {
        book__c buuk=new book__c();
        buk.add(buuk);
        return null;
    }
}
Please let me know if this helps.

Regards,
Mahesh
 
aditya naiduaditya naidu
HI JeffreyStevens,
there are multiple records in book__c, and i wanna fetch a particular record from the S-object again when i click at add row there will be empty row fields displayed and i want to mention a particular book name and fetch it again but here in my scenario i am unable to do it, When ever i click add row and try to fetch the details it is automatically refreshing and going to the first row, Kindly advice how can i acheive it.
aditya naiduaditya naidu
HI mahesh,
Thanks for responding to my query, I am facing the same issue.
In first row i am able to fetch the records by mentioning the name.
when i click at add row there will be empty row fields displayed and i want to mention a particular book name and fetch it again but here in my scenario i am unable to do it, When ever i click add row and try to fetch the details it is automatically refreshing and going to the first row, Kindly advice how can i acheive it.



Regards 
Aditya Naidu
aditya naiduaditya naidu
Hi mahesh,
I treied the code but  i am unable to fetch ,multiple records when i click add row and when i click fetch record automatically the page gets refreshed and goes to the first row
Tavva Sai KrishnaTavva Sai Krishna
Hi aditya,

I have modified the mahesh code in the fetch method slightly. Replace the fetch method in the mahes code with the below and give a try. I hope this gives you the solution. 
 
public pagereference fetch() { 
list<string > lstname = new list <string>(); 
for(book__c rec: buk) { 
   lstname.add (rec.name); 
} 
buk = new list<look>();   
buk=[select id,name,book_author__c,book_cost__c from book__c where name=:lstname];
return null;  
   }
let me know with errors, if  you face any .

NOTE: MARK THIS ANSWER AS BEST ANSWER IF IT ANSWERS TO YOUR QUESTION.

Regards,
Sai Krishna Tavva.