+ Start a Discussion
Raghu Sharma 6Raghu Sharma 6 

VF Page in Edit and Create Mode

I need to create a VF Page for Account for creating, viewing and editing records. What is the best approach to handle the fields for opening them for edit and view only mode? Can we use the same VF page to handle all the scenarios? How to code controller logic to handle multiple scnearios(save, edit and view)?
GauravTrivediGauravTrivedi
You can use inline edit funclionality on VF page. 
~Onkar~Onkar
You can use 2 pagepage for all the 3 action.

For new and Edit check record Id from parameter and based on the parameter you can render the page section.

1. Use standaerController and extension controller for use standard salesforce functionality,
2. Use dynamic Visualforce page for render the Edit (New and Edit Record) and detail page section.

 
Raghu Sharma 6Raghu Sharma 6
Thanks Onkar

a. If we were to use two VF pages, are you suggesting to use salesforce input fields for edit and create on VF page and use salesforce output field for viewing the records on VF page?

b. As you suggest to rerender the page, why do we need to use two pages?
 
Mahesh DMahesh D
Hi Raghu,

Please find the below code:

Visualforce Page:

 
<apex:page standardController="Account" extensions="AccountOverrideExtension">
    <apex:form>
        <apex:pageBlock id="EditPB" rendered="{!isEdit}">
            <apex:pageBlockSection>
                <apex:inputField value="{!acc.Name}"/>
                <apex:inputField value="{!acc.AccountNumber}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        
        <apex:pageBlock id="ViewPB" rendered="{!NOT(isEdit)}">
            <apex:pageBlockSection>
                <apex:outputField value="{!acc.Name}"/>
                <apex:outputField value="{!acc.AccountNumber}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
 
public class AccountOverrideExtension {

    public Account acc {get; set;}
    public Boolean isEdit {get; set;}
    public AccountOverrideExtension(ApexPages.StandardController controller) {
        acc = (Account) controller.getRecord();
        String modeStr = ApexPages.currentPage().getParameters().get('Mode');
        if(modeStr == 'Edit') {
            isEdit = true;
        } else {
            isEdit = false;
        }
        
        if(acc != null && acc.Id != null) {
            acc = [Select Id, Name, AccountNumber from Account where Id =: acc.Id];
            
        }
    }

}
Also tested the code as below:

If you pass the value Mode=Edit while calling the VF Page:

User-added image

If we don't pass the Mode while calling the VF Page:

User-added image


Please do let me know if it helps you.

Regards,
Mahesh
~Onkar~Onkar
Hi Raghu,

You can use single page.

To maintain the code and segregate two different actions, I was suggesting 2 page. In salesforce you achive multiple solution for single problem. 
 
~Thanks,
Onkar Kumar
Raghu Sharma 6Raghu Sharma 6
Thanks Mahesh for the code

How to redirect to standard page once the record is saved? what should be the approach to clone the record?
Mahesh DMahesh D
Hi Raghu,

As we are using the Standard Controller with Extension and we are not overriding the Save method, it will automatically redirect to Standard Page which is a defect behaviour of Save method.

Please do let me know if it helps you.

Regards,
Mahesh