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
Zach Gavin DelacroixZach Gavin Delacroix 

Editing Records in PageBlockTable.

Hello Experts,

I'm new to visualforce page and trying to explore things by doing some tests. I'd like to ask for your help on how to I'll be able to edit records from a pageBlockTable in visualforce.

Here's how my page look like to start with.

Add Account Tab : Where I can save account (Shows accounts that are saved below)
User-added image

Account Details Tab: This shows all accounts I created (This is where my question is)

User-added image
 

Right now, I have the Edit and Delete commandLink but doesn't do anything yet. I don't know where to start here.

What I'd like to do is to be able to edit or delete the Account from those commandLinks.

Can I please have the Idea how to achieve that?

Thanks a in advance!

--MY CODES HERE--

=====Page=======
 

<apex:page controller="TheController" sidebar="false">
  <apex:form >
      <apex:pageBlock >
          <apex:tabPanel >
       
              <apex:tab label="Add Account">
                  <apex:pageBlock title="Input Account Details"> <br/>
                      <b>&nbsp;&nbsp;Account Name </b>
                      <br/>*&nbsp;<apex:inputText value="{!newAccount.name}"/>
                      <br/><apex:messages />
                      <p/><br/><b>&nbsp;&nbsp;Industry </b>
                      <br/>&nbsp;&nbsp;<apex:inputField value="{!newAccount.Industry}"/>
                      <p/><br/><b>&nbsp;&nbsp;Type</b><br/>
                      &nbsp;&nbsp;<apex:inputField value="{!newAccount.Type}"/>
                      <p/><br/><b>&nbsp;&nbsp;Phone </b><br/>
                      &nbsp;&nbsp;<apex:inputField value="{!newAccount.Phone}"/>
                      <p/>
                      <apex:commandButton value="Add Account" action="{!addAccount}" reRender="myPage"/>
                      
                  </apex:pageBlock>
                      
                  <apex:pageBlock title="Added Accounts" id="myPage">
                      <apex:pageBlockTable value="{!AddedAccountsList}" var="addedAccounts">
                          <apex:column value="{!addedAccounts.name}"/>
                          <apex:column value="{!addedAccounts.Industry}"/>
                          <apex:column value="{!addedAccounts.Type}"/>
                          <apex:column value="{!addedAccounts.Phone}"/>
                      </apex:pageBlockTable>
                  </apex:pageBlock>
              </apex:tab>
              
              <apex:tab label="Account Details">
                  <apex:pageBlockSection columns="1" >
                      <apex:pageBlockTable value="{!ViewAccounts}" var="acc" >
                          <apex:column ><apex:commandLink action="{!EditAccount}" value="Edit"/></apex:column>
                          <apex:column ><apex:commandLink action="{!DeleteAccount}" value="Delete"/></apex:column>
                          <apex:column value="{!acc.name}" />
                          <apex:column value="{!acc.Industry}"/>
                          <apex:column value="{!acc.Type}"/>
                          <apex:column value="{!acc.Phone}"/>
                      </apex:pageBlockTable>
                  </apex:pageBlockSection>
              </apex:tab>
              
          </apex:tabPanel>
      </apex:pageBlock>
  </apex:form>
</apex:page>
 


=====Controller=======

public class TheController {

    public Account newAccount{get;set;}
    list<Account> addedAccounts;
    
    public TheController(){
        newAccount = new Account();
        addedAccounts = new list<Account>();
    }

    public List<Account> getViewAccounts(){
        List<account> lstAccount = [select id, name, industry, type, phone from account where createdById =:userinfo.getuserId() order By CreatedDate desc];
        return lstAccount;
    }
    
    public void addAccount(){
            
             try {
                 addedAccounts.add(newAccount.clone());
                 insert newAccount.clone();
             } catch (Exception e) {
                 ApexPages.addMessages(e);
             }
             
    }
    
    public List<account> getAddedAccountsList(){
        return addedAccounts;
    }
    
    public void EditAccount(){
    
    }
    public void DeleteAccount(){
    }
}
MaKSinghMaKSingh
You should pass parameters to commandlink
<apex:column ><apex:commandLink action="{!EditAccount}" value="Edit"/>
<apex:param name="AccountId"
                value="{!acc.id}"
                assignTo="{!AccountId}"/>
        </apex:commandLink>
</apex:column>

And then in controller
String AccountId;

public void setAccountId(String val)
{
this.AccountId=val;
}
public String getAccountId()
{
return this.AccountId;
}
And Now you can implement Account Edit & delete operations
public void DeleteAccount()
{
Account acc=[SELECT ID FROM Account WHERE id=:this.AccountId];
DELETE acc;
    }

this should delete your account
You can similarly do for edit option.