Tuesday 4 February 2014

A Visualforce Page Excatly Same as Contact Edit Page

Hi 
This is a very simple post. I had seen that question  developerforce.com . That's why I thought I will share in my blog. The question was there will be a link called "Copy Mailing Address to Other Address" when clicked that link all Mailing address will be copied to other address same as standard edit page of contact.
This can be done two ways. 
  • By using java script
  • By using Controller extension
In the command link we have to call java script method there we will get all the field value of mailing addresss using documnet.getElementByid() method in javascript and will assign to the other address field. 

Second approach is we will call extension method where will assign all the mailing address field to other address field and will reRender (refresh) the block. It will behave exactly like as standard edit page layout of Contact functionality.
Now the challenge is how will we display that command link inside that pagablock section header.

This is the page and controller 


<apex:page standardController="Contact" extensions="contactEditPageExtension">
    <apex:sectionHeader title="Contact Edit" subtitle="New Contact"/>
    <apex:outputText value="Contacts not associated with accounts are private and cannot be viewed by other users or included in reports."> 
    </apex:outputText><br/><br/>
    <apex:form >
        <apex:pageBlock title="Contact Edit" id="test">
            <apex:pageBlockButtons location="both">
                <apex:commandButton value="Save" action="{!Save}"/>
                <apex:commandButton value="Save & New" />
                <apex:commandButton value="Cancel" action="{!Cancel}"/>
                
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Contact Information">
                <apex:inputField value="{!contact.FirstName}"/> 
                <apex:inputField value="{!contact.LastName}"/>       
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Test">
                   <apex:facet name="header">
                        <apex:pageBlockSectionItem >
                        <apex:outputText value="Address Information" style="float:left;"></apex:outputText>
                        <apex:commandLink value="Copy Malling Address to Other Address" style="float:right;" action="{!test}" reRender="test"/>
                        </apex:pageBlockSectionItem>
                    </apex:facet>
             
                    <apex:inputField value="{!contact.MailingCountry}"/> 
                    <apex:inputField value="{!contact.otherCountry}"/>  
                    <apex:inputField value="{!contact.MailingStreet}"/> 
                    <apex:inputField value="{!contact.otherStreet}"/>   
                    <apex:inputField value="{!contact.MailingState}"/> 
                    <apex:inputField value="{!contact.otherState}"/>    
                  
            </apex:pageBlockSection>
             
        </apex:pageBlock>
    </apex:form>
  </apex:page>


My controller 


public with sharing class contactEditPageExtension {
    public Contact contact{get;set;}
    public contactEditPageExtension(ApexPages.StandardController controller) {
        //this.contact = (Contact )controller.getRecord();
        contact = new Contact();
    }
    
    public void test(){
         contact.otherCountry  =  contact.MailingCountry;
        contact.otherState  =   contact.MailingState;
        contact.otherStreet  =   contact.MailingStreet;
        //return null;
    }


}

1 comment:

Unknown said...

Thanku bya,it is very useful to us