1

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.

enter image description here 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????

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="one"/> 
                        <apex:param value="{!a.id}" assignTo="{!accid}"/> 
                </apex:column> 
            </apex:pageBlockTable> 
            <apex:pageBlockSection > 
                <apex:pageBlockSectionItem > 
                ID:{!accid} 
                </apex:pageBlockSectionItem> 
            </apex:pageBlockSection> 
            </apex:pageBlock> 
    </apex:form> 
</apex:page>

Class

public class one{ 

    public string acName{set;get;} 
    public integer acPhone{set;get;} 
    public List<Account> accs{set;get;} 
    public string accid {set;get;} 

    public pagereference save(){ 
        //accid = apexpages.currentPage().getParameters().get('id'); 
        accs = [select id,name,phone,type from account limit 5]; 
        return null; 
    } 
}
0

SO as per your updated code.

Issue in your command button. Your apex:param should be inside command button and give param name as well. This should work for you.

<apex:commandButton value="click" action="{!save}" reRender="one"> 
      <apex:param name="accountId" value="{!a.id}" assignTo="{!accid}"/> 
</apex:commandButton>
0

You're re-rendering the page block. your string Id is outside the page block. Try doing this change.

<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:pageBlockSection>
        <apex:pageBlockSectionItem>
              ID:{!accid}
        <apex:pageBlockSectionItem>
    <apex:pageBlockSection>         
  </apex:pageBlock>      
</apex:form>

  • Hi Amr Ibrahim, I may think so, this would not be work. because page block is just to display something in a block of section. – KS Kumaar Jul 28 '16 at 6:07
  • @KSKumaar I've updated the code ... my idea is the issue is with re-rendering – Amr Ibrahim Jul 28 '16 at 6:31
  • I followed your instructions. No results came even changed the code. – KS Kumaar Jul 28 '16 at 6:41
  • 1
    Thanks Amr Ibrahim, With help of you and @Ratan. I got solution to this. – KS Kumaar Jul 28 '16 at 9:54
0

apex:Param doesn't work with apex:commandButton as it work with apex:commandLink

Need to rerender a dummy element in your page, which helps to passes the correct value to controller method.

See blog on the same from Jeff Douglas. This will help.

May be you can try below code:

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="{!click}" rerender="dummy" >
                        <apex:param value="{!a.id}" assignTo="{!accId}" name="paramId"/>
                     </apex:commandButton>   
                </apex:column>

            </apex:pageBlockTable>

        </apex:pageBlock>  
    </apex:form>
</apex:page>

Apex Class:

public class one{

    public List<Account> accs {get;set;}
    // the variable being set from the commandbutton
    public String accId  {get;set;}


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

        return null;
    }

    public Pagereference click(){
        System.debug('###'+accid);
        return null;
    }

}
0

Pass Your parameter value inside command button. Try below code snippet.

<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 assignTo="{!accid}" value="{!a.id}"/>
                    </apex:commandButton>
                </apex:column>
                </apex:pageBlockTable>
      </apex:pageBlock>
      <apex:outputPanel id="two">
            ID:{!accid}
        </apex:outputPanel> 
    </apex:form>
</apex:page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.