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
KS KumaarKS Kumaar 

how to get record id using button click in visualforce page


I have a VF page which contains record id, name of account object and a button for each and every record as shown in image.User-added image
If i click the button for particular record, the respected record id should be displayed in same visualforce page. For that i created a class and VF page as given below.

VF Page:
<apex:page controller="one" action="{!save}">
    <apex:form >
        <apex:pageBlock id="one">
            <apex:pageBlockTable value="{!accs}" var="a">
                <apex:column value="{!a.id}"/>
                <apex:column value="{!a.name}"/>
                <apex:column >
                    <apex:commandButton value="click" action="{!save}" reRender="one"/>
                    <apex:param assignTo="{!accid}" value="{!a.id}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>  
        ID:{!accid}
    </apex:form>
</apex:page>
Controller:
public class one{
    public string acName{set;get;}
    public integer acPhone{set;get;}
    public List<Account> accs{set;get;}
    public ID accid {set;get;}

    public pagereference save(){
        accs = [select id,name,phone,type from account limit 5];

        return null;
    }
}
I am unable to get this requirement. Could anyone please help me in this????
Thanking you
KS Kumaar
Best Answer chosen by KS Kumaar
KS KumaarKS Kumaar

Thanks everyone. Finally i got my answer with someone help. Here is my updated code.

<apex:page controller="one" action="{!save}">
    <apex:form >
        <apex:pageBlock id="one">
            <apex:pageBlockTable value="{!accs}" var="a">
                <apex:column value="{!a.id}"/>
                <apex:column value="{!a.name}"/>
                <apex:column >
                    <apex:commandButton value="click" action="{!save}" reRender="two">
                    <apex:param name="accountId" value="{!a.id}" assignTo="{!accid}"/>
                    </apex:commandButton>
                </apex:column>
            </apex:pageBlockTable>
            </apex:pageBlock>
            <apex:pageBlock id="two">
            <apex:pageBlockSection >
        <apex:pageBlockSectionItem >
              ID:{!accid}
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection> 
        </apex:pageBlock>
     </apex:form>
</apex:page>
No need any changes on Controller
Thanks 
KS Kumaar

All Answers

PiotrSobczakPiotrSobczak
Hi,

Use wrapper class http://salesforce-walker.blogspot.com/2013/12/row-edit-and-save-using-wrapper-class.html
KS KumaarKS Kumaar

Hi PiotrSobczak,
Where you were totally using indexes. Never used record id.

thanks for your help. Your help is appreciable.
 

Nitin SharmaNitin Sharma
Hello KS,

Use this code for query  in ur controller class it will work.
accs = [select id, name, site from Account
 where id = :ApexPages.currentPage().getParameters().get('id')];

 Thanks,
Nitin Sharma
KS KumaarKS Kumaar
Hi Nitin Sharma, I changed modifications as you said. But could not get any changes....!
KS KumaarKS Kumaar

Thanks everyone. Finally i got my answer with someone help. Here is my updated code.

<apex:page controller="one" action="{!save}">
    <apex:form >
        <apex:pageBlock id="one">
            <apex:pageBlockTable value="{!accs}" var="a">
                <apex:column value="{!a.id}"/>
                <apex:column value="{!a.name}"/>
                <apex:column >
                    <apex:commandButton value="click" action="{!save}" reRender="two">
                    <apex:param name="accountId" value="{!a.id}" assignTo="{!accid}"/>
                    </apex:commandButton>
                </apex:column>
            </apex:pageBlockTable>
            </apex:pageBlock>
            <apex:pageBlock id="two">
            <apex:pageBlockSection >
        <apex:pageBlockSectionItem >
              ID:{!accid}
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection> 
        </apex:pageBlock>
     </apex:form>
</apex:page>
No need any changes on Controller
Thanks 
KS Kumaar
This was selected as the best answer