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
Abhishek Sharma 527Abhishek Sharma 527 

Editable records in visualforce

Hello Folks, I have created Visualforce page which shows records of account and it's editable also there's checkbox but now requirement is to make records editable only when checkbox is checked, for example if i check 3 checkbox out of 10 records then only those 3 should be editable.

can anyone help in fufilling this requirement.

//my VF code 

<apex:page standardController="account" sidebar="false" recordSetVar="records">
    
    <apex:form>
        <apex:pageBlock>
        <apex:pageBlockTable value="{!records}" var="rec">
            <apex:column headervalue="checkbox">
                <apex:inputCheckbox/>
            </apex:column>
            <apex:column>
                <apex:outputField label="name" value="{!rec.name}"/>
                <apex:facet name="header">Account name</apex:facet>
            </apex:column>
            <apex:column>
                <apex:outputField label="industry" value="{!rec.industry}"/>
            <apex:facet name="header">Industry</apex:facet>
            </apex:column>
            
            <apex:inlineEditSupport event="ondblclick"/>
            </apex:pageBlockTable>
            <apex:pageBlockButtons location="Bottom">
            <apex:commandButton value="Save" action="{!save}" id="saveButton"/>
                <apex:commandButton value="Edit" action="{!save}" id="editButton"/>
                <apex:commandButton value="Cancel" action="{!cancel}" id="delButton"/>
                </apex:pageBlockButtons>
            
             </apex:pageBlock>
    </apex:form>
</apex:page>

 
Best Answer chosen by Abhishek Sharma 527
AnkaiahAnkaiah (Salesforce Developers) 
Hi Abhishek,

try with below code.

VF Page:
<apex:page controller="EditAccount_controller" tabStyle="Account">
<apex:form id="frm">
<apex:sectionHeader title="Account Records"/>
<apex:actionFunction name="check" reRender="Acc_Table"/>
<apex:pageBlock >

<apex:pageBlockButtons >
<apex:commandButton value="Update Record" action="{!updateRecord}" reRender="frm"/>
</apex:pageBlockButtons>
<apex:outputPanel id="Acc_Table">
<table width = "100%" id="tab">
<tr>
<th> Action </th>
<th> Name </th>
<th> Industry </th>
<th> Account Number </th>
<th> Phone </th>
</tr>

<apex:repeat value="{!wrapClassList}" var="AccountTable" >
<tr>

<td> <apex:inputCheckbox value="{!AccountTable.flag}" onClick="check()"/> </td>

<td> <apex:outputField value="{!AccountTable.conWrap.Name}" rendered="{!(!AccountTable.flag)}" />
<apex:inputField value="{!AccountTable.conWrap.Name}" rendered="{!AccountTable.flag}" />
</td>

<td> <apex:outputField value="{!AccountTable.conWrap.Industry}" rendered="{!(!AccountTable.flag)}"/>
<apex:inputField value="{!AccountTable.conWrap.Industry}" rendered="{!AccountTable.flag}"/>
</td>

<td> <apex:outputField value="{!AccountTable.conWrap.AccountNumber}" rendered="{!(!AccountTable.flag)}"/>
<apex:inputField value="{!AccountTable.conWrap.AccountNumber}" rendered="{!AccountTable.flag}"/>
</td>

<td> <apex:outputField value="{!AccountTable.conWrap.phone}" rendered="{!(!AccountTable.flag)}"/>
<apex:inputField value="{!AccountTable.conWrap.phone}" rendered="{!AccountTable.flag}"/>
</td>
</tr>
</apex:repeat>

</table>
</apex:outputPanel>
</apex:pageBlock>

</apex:form>

</apex:page>

Apex Class:
public class EditAccount_controller{
public List <wrapperClass >wrapClassList{get;set;}
public List <Account>AccList{get;set;}
public wrapperClass wc{get;set;}

public EditAccount_controller(){
wrapClassList = new List<wrapperClass>();
AccList = new List<Account>();

/* Make Sure used proper field if you are using custom field and custom object. we have used standard fields Id, Name, Industry, Phone of Account standard Object */
AccList = [SELECT Id, name, Industry,AccountNumber, Phone FROM Account LIMIT 10];

for(Account acc : AccList){
wc = new wrapperClass();
wc.flag = false;
wc.accWrap = acc;
wrapClassList.add(wc);
}

}

public pagereference updateRecord() {
Upsert AccList;
//After Upsert     refresh the same page
return new pageReference('/apex/EditAccount').setRedirect(true);

}

/* Wrapper Class is used to bind the other things for object like we bind checkbox before every row record */
public class wrapperClass{
public boolean flag{get;set;}
public Account accWrap{get;set;}

}
}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​​​​​​​​

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Abhishek,

Refer the below link will help you to resolve the issue.
https://cloudyvipin.wordpress.com/2017/02/16/inline-edit-record-when-checkbox-checked-on-visualforce-page/

If still facing an issue, let me know will help you with code.

Thanks!!
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Abhishek,

try with below code.

VF Page:
<apex:page controller="EditAccount_controller" tabStyle="Account">
<apex:form id="frm">
<apex:sectionHeader title="Account Records"/>
<apex:actionFunction name="check" reRender="Acc_Table"/>
<apex:pageBlock >

<apex:pageBlockButtons >
<apex:commandButton value="Update Record" action="{!updateRecord}" reRender="frm"/>
</apex:pageBlockButtons>
<apex:outputPanel id="Acc_Table">
<table width = "100%" id="tab">
<tr>
<th> Action </th>
<th> Name </th>
<th> Industry </th>
<th> Account Number </th>
<th> Phone </th>
</tr>

<apex:repeat value="{!wrapClassList}" var="AccountTable" >
<tr>

<td> <apex:inputCheckbox value="{!AccountTable.flag}" onClick="check()"/> </td>

<td> <apex:outputField value="{!AccountTable.conWrap.Name}" rendered="{!(!AccountTable.flag)}" />
<apex:inputField value="{!AccountTable.conWrap.Name}" rendered="{!AccountTable.flag}" />
</td>

<td> <apex:outputField value="{!AccountTable.conWrap.Industry}" rendered="{!(!AccountTable.flag)}"/>
<apex:inputField value="{!AccountTable.conWrap.Industry}" rendered="{!AccountTable.flag}"/>
</td>

<td> <apex:outputField value="{!AccountTable.conWrap.AccountNumber}" rendered="{!(!AccountTable.flag)}"/>
<apex:inputField value="{!AccountTable.conWrap.AccountNumber}" rendered="{!AccountTable.flag}"/>
</td>

<td> <apex:outputField value="{!AccountTable.conWrap.phone}" rendered="{!(!AccountTable.flag)}"/>
<apex:inputField value="{!AccountTable.conWrap.phone}" rendered="{!AccountTable.flag}"/>
</td>
</tr>
</apex:repeat>

</table>
</apex:outputPanel>
</apex:pageBlock>

</apex:form>

</apex:page>

Apex Class:
public class EditAccount_controller{
public List <wrapperClass >wrapClassList{get;set;}
public List <Account>AccList{get;set;}
public wrapperClass wc{get;set;}

public EditAccount_controller(){
wrapClassList = new List<wrapperClass>();
AccList = new List<Account>();

/* Make Sure used proper field if you are using custom field and custom object. we have used standard fields Id, Name, Industry, Phone of Account standard Object */
AccList = [SELECT Id, name, Industry,AccountNumber, Phone FROM Account LIMIT 10];

for(Account acc : AccList){
wc = new wrapperClass();
wc.flag = false;
wc.accWrap = acc;
wrapClassList.add(wc);
}

}

public pagereference updateRecord() {
Upsert AccList;
//After Upsert     refresh the same page
return new pageReference('/apex/EditAccount').setRedirect(true);

}

/* Wrapper Class is used to bind the other things for object like we bind checkbox before every row record */
public class wrapperClass{
public boolean flag{get;set;}
public Account accWrap{get;set;}

}
}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​​​​​​​​
This was selected as the best answer
Abhishek Sharma 527Abhishek Sharma 527
Super !! my friend Ankaiah. it really helped.
Thank you very much