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
KMNKMN 

how to make editable Related record in vf page

HI,
I have Create a page like account form and add some related contacts
and i want make that contacts editable.
Can someone help i am practicing on it.
code...on standardcontroller
<apex:page standardController="Account">
    <style>
        .s1{Background-Color: LightBlue;}
        .s2{Background-Color: Pink;}
    </style>
    <apex:form >
        <apex:sectionHeader title="Account" subtitle="Account Form"/>
        <apex:pageBlock title="Account Form" >
            <apex:pageBlockSection title="Account Information" columns="2">    
                <apex:inputField value="{!Account.Owner.Name}" styleClass="s1"/>
                <apex:inputField value="{!Account.Rating}" style="BackGround-Color:Pink; color: blue;"/>
                <apex:inputField value="{!Account.Name}" styleClass="s1"/>
                <apex:inputField value="{!Account.Phone}" styleClass="s2"/> 
                <apex:inputField value="{!Account.ParentId}" styleClass="s1"/>
                <apex:inputField value="{!Account.Fax}" styleClass="s2"/> 
                <apex:inputField value="{!Account.AnnualRevenue}" styleClass="s1"/>
                <apex:inputField value="{!Account.NumberOfEmployees}" styleClass="s2"/>  
                <apex:inputField value="{!Account.Ownership}" styleClass="s1"/>
                 <apex:inputField value="{!Account.website}" styleClass="s2"/>  
                
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Additional Information" columns="2">    
                <apex:inputField value="{!Account.Site}" styleClass="s1"/>
                <apex:inputField value="{!Account.AccountNumber}" styleClass="s2"/>
                <apex:inputField value="{!Account.Industry}" styleClass="s2"/>
                <apex:inputField value="{!Account.Type}" styleClass="s1"/> 
            </apex:pageBlockSection>           
            <apex:pageBlockSection title="Address Information" columns="2">  
                <apex:inputField value="{!Account.BillingStreet}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingStreet}" styleClass="s2"/>
                <apex:inputField value="{!Account.BillingState}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingState}" styleClass="s2"/>
                <apex:inputField value="{!Account.BillingCity}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingCity}" styleClass="s2"/>
                <apex:inputField value="{!Account.BillingPostalCode}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingPostalCode}" styleClass="s2"/>
                <apex:inputField value="{!Account.BillingCountry}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingCountry}" styleClass="s2"/>
            </apex:pageBlockSection>            
            <apex:pageBlockSection title="Description" columns="2">    
                <apex:inputField value="{!Account.Description}" styleClass="s1"/>
            </apex:pageBlockSection> 
            <apex:pageBlock title="Related Contacts">
    
                <apex:pageBlockTable value="{!Account.contacts}" var="con">
                    <apex:column value="{!con.firstName}"/>
                    <apex:column value="{!con.LastName}"/>
                    <apex:column value="{!con.Phone}"/>
                    <apex:column value="{!con.AccountId}"/>         
                </apex:pageBlockTable>

                </apex:pageBlock>
            
            <apex:pageBlockButtons location="Bottom">
                <apex:commandButton action="{!Save}" value="Save"/>
                <apex:commandButton action="{!Cancel}" value="Cancel"/>          
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
Best Answer chosen by KMN
Maharajan CMaharajan C
Hi Khalid,

You have to extension contoller to update the related contacts:

Please try the below codes:

Visualforce Page:
<apex:page standardController="Account" extensions="forumAccountContactInlineController">
    <style>
        .s1{Background-Color: LightBlue;}
        .s2{Background-Color: Pink;}
    </style>
    <apex:form >
        <apex:sectionHeader title="Account" subtitle="Account Form"/>
        <apex:pageBlock title="Account Form" >
            <apex:pageBlockSection title="Account Information" columns="2">    
                <apex:inputField value="{!Account.Owner.Name}" styleClass="s1"/>
                <apex:inputField value="{!Account.Rating}" style="BackGround-Color:Pink; color: blue;"/>
                <apex:inputField value="{!Account.Name}" styleClass="s1"/>
                <apex:inputField value="{!Account.Phone}" styleClass="s2"/> 
                <apex:inputField value="{!Account.ParentId}" styleClass="s1"/>
                <apex:inputField value="{!Account.Fax}" styleClass="s2"/> 
                <apex:inputField value="{!Account.AnnualRevenue}" styleClass="s1"/>
                <apex:inputField value="{!Account.NumberOfEmployees}" styleClass="s2"/>  
                <apex:inputField value="{!Account.Ownership}" styleClass="s1"/>
                <apex:inputField value="{!Account.website}" styleClass="s2"/>  
                
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Additional Information" columns="2">    
                <apex:inputField value="{!Account.Site}" styleClass="s1"/>
                <apex:inputField value="{!Account.AccountNumber}" styleClass="s2"/>
                <apex:inputField value="{!Account.Industry}" styleClass="s2"/>
                <apex:inputField value="{!Account.Type}" styleClass="s1"/> 
            </apex:pageBlockSection>           
            <apex:pageBlockSection title="Address Information" columns="2">  
                <apex:inputField value="{!Account.BillingStreet}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingStreet}" styleClass="s2"/>
                <apex:inputField value="{!Account.BillingState}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingState}" styleClass="s2"/>
                <apex:inputField value="{!Account.BillingCity}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingCity}" styleClass="s2"/>
                <apex:inputField value="{!Account.BillingPostalCode}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingPostalCode}" styleClass="s2"/>
                <apex:inputField value="{!Account.BillingCountry}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingCountry}" styleClass="s2"/>
            </apex:pageBlockSection>            
            <apex:pageBlockSection title="Description" columns="2">    
                <apex:inputField value="{!Account.Description}" styleClass="s1"/>
            </apex:pageBlockSection> 
            <apex:pageBlock title="Related Contacts">
                
                <apex:pageBlockTable value="{!cons}" var="con">
                    <apex:column headervalue="First Name">
                        <apex:inputField  value="{!con.firstname}"/>
                    </apex:column>
                    <apex:column headervalue="Last Name">
                        <apex:inputField  value="{!con.LastName}"/>
                    </apex:column>
                    <apex:column headervalue="Phone">
                        <apex:inputField  value="{!con.Phone}"/>
                    </apex:column>
                    <apex:column value="{!con.AccountId}"/>         
                </apex:pageBlockTable>
                
            </apex:pageBlock>
            
            <apex:pageBlockButtons location="Bottom">
                <apex:commandButton action="{!SaveAll}" value="Save"/>
                <apex:commandButton action="{!Cancel}" value="Cancel"/>          
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:
 
public class forumAccountContactInlineController {
    
    ApexPages.StandardController controller;
    public List<contact> cons{get; set;}
    Public account acc = new Account();
    public forumAccountContactInlineController(ApexPages.StandardController stdcon){
        controller = stdcon;
        cons = new List<Contact>();
        acc = (Account)controller.getRecord();
        cons = [Select firstName,LastName,Phone,Accountid from Contact where accountId =: acc.Id];
    }
    
    public PageReference SaveAll() {
        if(!cons.isEmpty()){
            update cons;
        }
        return controller.save();
    }
    
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Khalid,

You have to extension contoller to update the related contacts:

Please try the below codes:

Visualforce Page:
<apex:page standardController="Account" extensions="forumAccountContactInlineController">
    <style>
        .s1{Background-Color: LightBlue;}
        .s2{Background-Color: Pink;}
    </style>
    <apex:form >
        <apex:sectionHeader title="Account" subtitle="Account Form"/>
        <apex:pageBlock title="Account Form" >
            <apex:pageBlockSection title="Account Information" columns="2">    
                <apex:inputField value="{!Account.Owner.Name}" styleClass="s1"/>
                <apex:inputField value="{!Account.Rating}" style="BackGround-Color:Pink; color: blue;"/>
                <apex:inputField value="{!Account.Name}" styleClass="s1"/>
                <apex:inputField value="{!Account.Phone}" styleClass="s2"/> 
                <apex:inputField value="{!Account.ParentId}" styleClass="s1"/>
                <apex:inputField value="{!Account.Fax}" styleClass="s2"/> 
                <apex:inputField value="{!Account.AnnualRevenue}" styleClass="s1"/>
                <apex:inputField value="{!Account.NumberOfEmployees}" styleClass="s2"/>  
                <apex:inputField value="{!Account.Ownership}" styleClass="s1"/>
                <apex:inputField value="{!Account.website}" styleClass="s2"/>  
                
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Additional Information" columns="2">    
                <apex:inputField value="{!Account.Site}" styleClass="s1"/>
                <apex:inputField value="{!Account.AccountNumber}" styleClass="s2"/>
                <apex:inputField value="{!Account.Industry}" styleClass="s2"/>
                <apex:inputField value="{!Account.Type}" styleClass="s1"/> 
            </apex:pageBlockSection>           
            <apex:pageBlockSection title="Address Information" columns="2">  
                <apex:inputField value="{!Account.BillingStreet}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingStreet}" styleClass="s2"/>
                <apex:inputField value="{!Account.BillingState}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingState}" styleClass="s2"/>
                <apex:inputField value="{!Account.BillingCity}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingCity}" styleClass="s2"/>
                <apex:inputField value="{!Account.BillingPostalCode}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingPostalCode}" styleClass="s2"/>
                <apex:inputField value="{!Account.BillingCountry}" styleClass="s1"/>
                <apex:inputField value="{!Account.ShippingCountry}" styleClass="s2"/>
            </apex:pageBlockSection>            
            <apex:pageBlockSection title="Description" columns="2">    
                <apex:inputField value="{!Account.Description}" styleClass="s1"/>
            </apex:pageBlockSection> 
            <apex:pageBlock title="Related Contacts">
                
                <apex:pageBlockTable value="{!cons}" var="con">
                    <apex:column headervalue="First Name">
                        <apex:inputField  value="{!con.firstname}"/>
                    </apex:column>
                    <apex:column headervalue="Last Name">
                        <apex:inputField  value="{!con.LastName}"/>
                    </apex:column>
                    <apex:column headervalue="Phone">
                        <apex:inputField  value="{!con.Phone}"/>
                    </apex:column>
                    <apex:column value="{!con.AccountId}"/>         
                </apex:pageBlockTable>
                
            </apex:pageBlock>
            
            <apex:pageBlockButtons location="Bottom">
                <apex:commandButton action="{!SaveAll}" value="Save"/>
                <apex:commandButton action="{!Cancel}" value="Cancel"/>          
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:
 
public class forumAccountContactInlineController {
    
    ApexPages.StandardController controller;
    public List<contact> cons{get; set;}
    Public account acc = new Account();
    public forumAccountContactInlineController(ApexPages.StandardController stdcon){
        controller = stdcon;
        cons = new List<Contact>();
        acc = (Account)controller.getRecord();
        cons = [Select firstName,LastName,Phone,Accountid from Contact where accountId =: acc.Id];
    }
    
    public PageReference SaveAll() {
        if(!cons.isEmpty()){
            update cons;
        }
        return controller.save();
    }
    
}

Thanks,
Maharajan.C
This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47
Hi Khalid Ameen,
You can use custom controller to perform on this requirement
you are going to have to create an Apex Controller on the backend and call it at Save button click.
you can get reference from this link:
Click On --> link (https://developer.salesforce.com/forums/?id=906F0000000BXFkIAO)
If you find your Solution then mark this as the best answer.   
Thank you!  
Regards,  
Suraj Tripathi
KMNKMN
Hi Maharajan C,
Your code good working fine but it take me to the UI page.
how can i do that whole process should be on vf page only
after click save button should not go on UI page
Maharajan CMaharajan C
Change the apex class like below:
 
public class forumAccountContactInlineController {
    
    ApexPages.StandardController controller;
    public List<contact> cons{get; set;}
    Public account acc = new Account();
    public forumAccountContactInlineController(ApexPages.StandardController stdcon){
        controller = stdcon;
        cons = new List<Contact>();
        acc = (Account)controller.getRecord();
        if(acc.id != null)
            cons = [Select firstName,LastName,Phone,Accountid from Contact where accountId =: acc.Id];
    }
    
    public PageReference SaveAll() {
        if(!cons.isEmpty()){
            update cons;
        }
        upsert this.acc;
        return null;
    }
    
}

Thanks,
Maharajan.C
KMNKMN
Thank You Maharajan C.
For your help and Time..