Copy Billing Address to Shipping Address in Salesforce

How to Copy Billing Address to Shipping Address in Salesforce?

When we create new Contract and edit them them there is a Standard Functionality that Copies Billing Address to Shipping Address.But if we need to implement the same in a Visualforce Page then you can do it in following way :

u

v

<apex:page standardController="Contact" extensions="yourExtension" id="pageContId">
    <script type="text/javascript">
    function billinfToShippingAddr() {
        // Variables for Billing Address
        var billPostCode = document.getElementById('pageContId:contactForm:contId:pbsBillingAdd:ifBPstCode').value;
        var billAddr1 =document.getElementById('pageContId:contactForm:contId:pbsBillingAdd:ifBAddress1').value;
        var billAddr2 = document.getElementById('pageContId:contactForm:contId:pbsBillingAdd:ifBAddress2').value;
        var billAddr3 = document.getElementById('pageContId:contactForm:contId:pbsBillingAdd:ifBAddress3').value;
        var billCity = document.getElementById('pageContId:contactForm:contId:pbsBillingAdd:ifBillCity').value;
        var billState = document.getElementById('pageContId:contactForm:contId:pbsBillingAdd:ifBillState').value;
        var billCtry = document.getElementById('pageContId:contactForm:contId:pbsBillingAdd:ifBillCountry').value;
        // Copying the Billing Address to the Shipping Address
        if(billPostCode != null) {
            document.getElementById('pageContId:contactForm:contId:pbsShippingAdd:ifShipPostCode').value = billPostCode;
        }
        if(billAddr1 != null) {
            document.getElementById('pageContId:contactForm:contId:pbsShippingAdd:ifShipAdd1').value = billAddr1;
        }
        if(billAddr2 != null) {
            document.getElementById('pageContId:contactForm:contId:pbsShippingAdd:ifShipAdd2').value = billAddr2;
        }
        if(billAddr3 != null) {
            document.getElementById('pageContId:contactForm:contId:pbsShippingAdd:ifShipAdd3').value = billAddr3;
        }
        if(billCity != null) {
            document.getElementById('pageContId:contactForm:contId:pbsShippingAdd:ifShipCity').value = billCity;
        }
        if(billState != null) {
            document.getElementById('pageContId:contactForm:contId:pbsShippingAdd:ifShipState').value = billState;
        }
        if(billCtry != null) {
            document.getElementById('pageContId:contactForm:contId:pbsShippingAdd:ifShipCountry').value = billCtry;
        }
    }
    </script>
    <apex:form id="contactForm">
        <apex:sectionHeader title="Contact Info" subtitle="{!Contact.Name}"/>
        <apex:pageBlock mode="Edit" id="contId">
            <apex:pageBlockSection showHeader="true" collapsible="false" columns="2" title="Billing Address Information" id="pbsBillingAdd">
                <apex:inputField value="{!Contact.Billing_Postal_Code__c}" id="ifBPstCode"/> <apex:inputField value="{!Contact.Billing_City__c}" id="ifBillCity"/>
                <apex:inputField value="{!Contact.Billing_Address_Line_1__c}" id="ifBAddress1"/>
                <apex:inputField value="{!Contact.Billing_State__c}" id="ifBillState"/>
                <apex:inputField value="{!Contact.Billing_Address_Line_2__c}" id="ifBAddress2"/>
                <apex:inputField value="{!Contact.Billing_Country__c}" id="ifBillCountry"/>
                <apex:inputField value="{!Contact.Billing_Address_Line_3__c}" id="ifBAddress3"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection showHeader="true" collapsible="false" columns="2" title="Shipping Address Information" id="pbsShippingAdd">
                <apex:inputField value="{!Contact.Shipping_Postal_Code__c}" id="ifShipPostCode"/> <a HREF="#" onClick="return billinfToShippingAddr();">Copy Billing Address to Shipping Address</a>
                <apex:inputField value="{!Contact.Shipping_Address_Line_1__c}" id="ifShipAdd1"/>
                <apex:inputField value="{!Contact.Shipping_City__c}" id="ifShipCity"/>
                <apex:inputField value="{!Contact.Shipping_Address_Line_2__c}" id="ifShipAdd2"/>
                <apex:inputField value="{!Contact.Shipping_State__c}" id="ifShipState"/>
                <apex:inputField value="{!Contact.Shipping_Address_Line_3__c}" id="ifShipAdd3"/>
                <apex:inputField value="{!Contact.Shipping_Country__c}" id="ifShipCountry"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Cheers!!!

Popular Salesforce Blogs