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
MaggieSumitMaggieSumit 

How add edit field option on these code?

<apex:page standardController="Account" extensions="contrller">
  <apex:form >
    <apex:pageblock >
       <!-- Display Account related Contacts -->
       <apex:pageBlockTable value="{!relatedContacts}" var="val">
         <apex:column value="{!val.name}"/>
         <apex:column value="{!val.email}" />
         <apex:inputField value="{!val.name}"/>
       </apex:pageBlockTable> 
    </apex:pageblock>
  </apex:form>
</apex:page>

public with sharing class contrller {
Public id Current_Acc_Id;

    public contrller(ApexPages.StandardController controller) {
    Current_Acc_Id = controller.getRecord().id;
    }

    public List<Contact> getrelatedContacts(){
        List <contact> conList = New List<Contact>();
        for(Account acc:[select id,name,(select name,id,email from contacts) from account where id=:Current_Acc_Id]){
           for(contact con:acc.contacts)
               conList.add(con); 
        }
        return conList;
    }
}
Best Answer chosen by MaggieSumit
Rohit K SethiRohit K Sethi
Hi Sumit kumar,

For New functionality update your code with below page and Controller code
<!--Page Code-->
<apex:pageblock id="pgb">
    <apex:pageBlockButtons >
        <apex:commandButton value="edit" action="{!editProcess}" rendered="{!Not(isEdit)}" reRender="pgb"/>
        <apex:commandButton value="save" action="{!save}" rendered="{!isEdit}" reRender="pgb"/>        
    </apex:pageBlockButtons>
       <apex:pageBlockTable value="{!lstContact}" var="val" rendered="{!Not(isEdit)}">
         <apex:column value="{!val.name}"/>
         <apex:column value="{!val.email}"/>
       </apex:pageBlockTable> 
       <apex:outputPanel rendered="{!Not(isEdit)}">
           <apex:commandButton action="{!addContact}" value="Add Contact" reRender="pgb"/>
       </apex:outputPanel>
        <apex:pageBlockTable value="{!lstContact}" var="val" rendered="{!(isEdit)}">
         <apex:column headerValue="FirstName">
             <apex:inputField value="{!val.FirstName}"/>
         </apex:column>
          <apex:column headerValue="LastName">
             <apex:inputField value="{!val.LastName}"/>
         </apex:column>
         <apex:column >
             <apex:inputField value="{!val.email}"/>
         </apex:column>
       </apex:pageBlockTable> 
    </apex:pageblock>
 
/*update Controller*/

public  void save(){
        
        if(lstContact.size() > 0){
            upsert lstContact;
            lstContact.clear();
        }
        
        for(Account acc:[select id,name,(select lastName,firstName,name,id,email from contacts) from account where id=:Current_Acc_Id]){
           for(contact con:acc.contacts)
               lstContact.add(con); 
        }
        isEdit = false;
    } 
    public void addContact(){
        lstContact.add(new Contact(AccountId = Current_Acc_Id));
        isEdit = true;
    }

Thanks.
 

All Answers

Rohit K SethiRohit K Sethi
Hi Sumit kumar,

You are using the name field in input field this field can't be as input because this field is composite type.So on edit you need to use the firstName and lastName field as inputField. 
And for better understanding i am posting the sample code with edit and save button which can be useful for you 
<!--Page-->
<apex:page standardController="Account" extensions="contrller">
  <apex:form>
    <apex:pageblock id="pgb">
    <apex:pageBlockButtons>
        <apex:commandButton value="edit" action="{!editProcess}" rendered="{!Not(isEdit)}" reRender="pgb"/>
        <apex:commandButton value="save" action="{!save}" rendered="{!isEdit}" reRender="pgb"/>        
    </apex:pageBlockButtons>
       <!-- Display Account related Contacts -->
       <apex:pageBlockTable value="{!lstContact}" var="val" rendered="{!Not(isEdit)}">
         <apex:column value="{!val.name}"/>
         <apex:column value="{!val.email}"/>
       </apex:pageBlockTable> 
        <apex:pageBlockTable value="{!lstContact}" var="val" rendered="{!(isEdit)}">
         <apex:column headerValue="FirstName">
             <apex:inputField value="{!val.FirstName}"/>
         </apex:column>
          <apex:column headerValue="LastName">
             <apex:inputField value="{!val.LastName}"/>
         </apex:column>
         <apex:column>
             <apex:inputField value="{!val.email}"/>
         </apex:column>
       </apex:pageBlockTable> 
    </apex:pageblock>
  </apex:form>
</apex:page>

/*Controller*/

public with sharing class contrller {
    Public id Current_Acc_Id;
    public Boolean isEdit { set; get;}
    public List<Contact> lstContact {set;get;}
    public contrller(ApexPages.StandardController controller) {
        Current_Acc_Id = controller.getRecord().id;
        isEdit = false;
        lstContact = New List<Contact>(); 
        for(Account acc:[select id,name,(select lastName,firstName,name,id,email from contacts) from account where id=:Current_Acc_Id]){
           for(contact con:acc.contacts)
               lstContact.add(con); 
        }
    }
    public void editProcess(){
        isEdit = true;
    }
    public  void save(){
        if(lstContact.size() > 0){
            update lstContact;
            lstContact.clear();
        }
        
        for(Account acc:[select id,name,(select lastName,firstName,name,id,email from contacts) from account where id=:Current_Acc_Id]){
           for(contact con:acc.contacts)
               lstContact.add(con); 
        }
        isEdit = false;
    } 
}

If this post solves your problem kindly mark it as solution.
Thanks
MaggieSumitMaggieSumit
Thanks Rohit, With this Code I am able to update the relationship object field, Can i create a new field there,
MaggieSumitMaggieSumit
Rohit Sethi 20: I just to create a new record under relationship field. I trying lost of way but getting unsuccess
Rohit K SethiRohit K Sethi
Yes you can use the fields in page as many you want but first you must to be add those field in query [Controller].
example :
(Account acc:[select id,name,(select lastName,firstName,name,id,email from contacts) from account where id=:Current_Acc_Id]
Rohit K SethiRohit K Sethi
Hi Sumit kumar,

For New functionality update your code with below page and Controller code
<!--Page Code-->
<apex:pageblock id="pgb">
    <apex:pageBlockButtons >
        <apex:commandButton value="edit" action="{!editProcess}" rendered="{!Not(isEdit)}" reRender="pgb"/>
        <apex:commandButton value="save" action="{!save}" rendered="{!isEdit}" reRender="pgb"/>        
    </apex:pageBlockButtons>
       <apex:pageBlockTable value="{!lstContact}" var="val" rendered="{!Not(isEdit)}">
         <apex:column value="{!val.name}"/>
         <apex:column value="{!val.email}"/>
       </apex:pageBlockTable> 
       <apex:outputPanel rendered="{!Not(isEdit)}">
           <apex:commandButton action="{!addContact}" value="Add Contact" reRender="pgb"/>
       </apex:outputPanel>
        <apex:pageBlockTable value="{!lstContact}" var="val" rendered="{!(isEdit)}">
         <apex:column headerValue="FirstName">
             <apex:inputField value="{!val.FirstName}"/>
         </apex:column>
          <apex:column headerValue="LastName">
             <apex:inputField value="{!val.LastName}"/>
         </apex:column>
         <apex:column >
             <apex:inputField value="{!val.email}"/>
         </apex:column>
       </apex:pageBlockTable> 
    </apex:pageblock>
 
/*update Controller*/

public  void save(){
        
        if(lstContact.size() > 0){
            upsert lstContact;
            lstContact.clear();
        }
        
        for(Account acc:[select id,name,(select lastName,firstName,name,id,email from contacts) from account where id=:Current_Acc_Id]){
           for(contact con:acc.contacts)
               lstContact.add(con); 
        }
        isEdit = false;
    } 
    public void addContact(){
        lstContact.add(new Contact(AccountId = Current_Acc_Id));
        isEdit = true;
    }

Thanks.
 
This was selected as the best answer
MaggieSumitMaggieSumit
Rohit Sethi 20: thanku so much, if i want use this code in Custome Object then what i need to chenge.

Lead and Question__c i have these two object. Question__c related with Lead, If use for this code where i need to change.
MaggieSumitMaggieSumit
public with sharing class contollertest {

    Public id Current_Lead_Id;
    public Boolean isEdit { set; get;}
    public List<Question__c> lstContact {set;get;}
    public contollertest (ApexPages.StandardController controller) {
        Current_Lead_Id = controller.getRecord().id;
        isEdit = false;
        lstContact = New List<Question__c>();
        for(Lead Lead:[select id,name,(select id,Questions__r.Name from Question__c) from Lead where id=:Current_Lead_Id]){
           for(Question__c con:acc.contacts)
               lstContact.add(con);
        }
    }

geeting this error message: Didn't understand relationship 'Question__c' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
Rohit K SethiRohit K Sethi
You are writing wrong query in relation use below 
Lead Lead:[select id,name,(select id,Name from Questions__r) from Lead where id=:Current_Lead_Id
MaggieSumitMaggieSumit
Now the proble solve i am getting this error message:Entity is not org-accessible
My Code: Please review my code
public with sharing class contollertest {

    Public id Current_le_Id;
    public Boolean isEdit { set; get;}
    public List<Question__c> lstQuestion {set;get;}
    public contollertest (ApexPages.StandardController controller) {
        Current_le_Id = controller.getRecord().id;
        isEdit = false;
        lstQuestion = New List<Question__c>();
        for(Lead le:[select id,name,(select id,Email__c from Questions__r) from lead where id=:Current_le_Id]){
           for(Question__c con:le.Question__c)
               lstQuestion.add(con);
        }
    }
    public void editProcess(){
        isEdit = true;
    }
    public  void save(){
        
        if(lstQuestion.size() > 0){
            upsert lstQuestion;
            lstQuestion.clear();
        }
        
        for(Lead le:[select id,name,(select id,Email__c from Questions__r) from lead where id=:Current_le_Id]){
           for(Question__c con:le.Question__c)
               lstQuestion.add(con);
        }
        isEdit = false;
    }
    public void addQuestion(){
        lstQuestion.add(new Question(leadId = Current_le_Id));
        isEdit = true;
    }
}
Rohit K SethiRohit K Sethi
I had some minor typo in my program that led me to the same error. Not sure if it's the same case with you. I forgot to add __c when calling a custom object and got the same error. Hope this helps!