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
SFDCExplorerSFDCExplorer 

How to copy data from one field to another field when check box is checked

I have some fields in my UserDetails__c object to display current address and permanant address. If both addresses are same then user can select the check box,so that perrmanant adress fields will get papulated automatically.I need to do this in vf page.Could any body help me here.
Best Answer chosen by SFDCExplorer
DeepthiDeepthi (Salesforce Developers) 
Hi,

You can achieve this functionality by using the <apex:actionSupport> tag.
apex:actionSupport:
"actionSupport" component adds AJAX support to other components in visualforce. It allows components to be refreshed asynchronously by calling the controller’s method when any event occurs (like click on button). It allows us to do partial page refresh asynchronously without refreshing  full page.
 
You can check the below sample code.

Visualforce Page:
<apex:page controller="ControllerClass">
<apex:form id="a">
    <apex:outputLabel >First Name</apex:outputLabel>
    <apex:inputField value="{!Cd.First_Name__c}"/> <br/>
    <apex:outputLabel >Last Name</apex:outputLabel>
    <apex:inputField value="{!Cd.Last_Name__c}"/>
    <apex:inputCheckbox value="{!selected1}">
        <apex:actionSupport action="{!copyField}" event="onclick" reRender="a" />
     </apex:inputCheckbox>
</apex:form>
</apex:page>
 
Controller Class:
public class ControllerClass {
    
    public boolean selected1 {get;set;}
    public Candidate__c Cd {get; set;}
    public PageReference copyField{get; set;}
    public ControllerClass(){
        Cd = new Candidate__c();
    }
    public PageReference copyField(){
        if(selected1==true){
        Cd.Last_Name__c = Cd.First_Name__c;
        }
        return null;
    }
}

Also, check the below links.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionSupport.htm 

http://www.sfdcpoint.com/salesforce/actionsupport-visualforce-salesforce/

Hope this helps you!
Best Regards,
Deepthi

All Answers

Mahesh DMahesh D
Hi

Please send me your existing VF page so that it will be easy to help you.

Here we have to use the <actionSupport tab on the check box, whne you select it and if the selection is true then call the controller method and copy the address fields and do reRender the page.

Please paste the existing VF page.

Regards,
Mahesh
Ravi Dutt SharmaRavi Dutt Sharma
Hey,

You can try below code. Let me know if it works. Thanks

VF Page :
<apex:page standardController="Budget__c" extensions="BudgetExt">
    <apex:form id="form">
        <apex:pageBlock >
            <apex:pageBlockSection id="block">
                <apex:inputField value="{!Budget__c.Present_Address__c}"/>
                <apex:inputCheckbox value="{!check}" label="Is Permanent address same as Present address">
                    <apex:actionSupport action="{!copyAddress}" reRender="form" event="onclick"/>
                </apex:inputCheckbox>
                <apex:inputField value="{!Budget__c.Permanent_Address__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Controller :
public with sharing class BudgetExt {
    
    public boolean check{get;set;}
    
    public Budget__c bud{get;set;}
    
    public BudgetExt(ApexPages.StandardController controller) {
        bud = (Budget__c)controller.getRecord();
    }
    
    public PageReference copyAddress(){
        bud.Permanent_Address__c = bud.Present_Address__c;
        return null;
    }

}

 
DeepthiDeepthi (Salesforce Developers) 
Hi,

You can achieve this functionality by using the <apex:actionSupport> tag.
apex:actionSupport:
"actionSupport" component adds AJAX support to other components in visualforce. It allows components to be refreshed asynchronously by calling the controller’s method when any event occurs (like click on button). It allows us to do partial page refresh asynchronously without refreshing  full page.
 
You can check the below sample code.

Visualforce Page:
<apex:page controller="ControllerClass">
<apex:form id="a">
    <apex:outputLabel >First Name</apex:outputLabel>
    <apex:inputField value="{!Cd.First_Name__c}"/> <br/>
    <apex:outputLabel >Last Name</apex:outputLabel>
    <apex:inputField value="{!Cd.Last_Name__c}"/>
    <apex:inputCheckbox value="{!selected1}">
        <apex:actionSupport action="{!copyField}" event="onclick" reRender="a" />
     </apex:inputCheckbox>
</apex:form>
</apex:page>
 
Controller Class:
public class ControllerClass {
    
    public boolean selected1 {get;set;}
    public Candidate__c Cd {get; set;}
    public PageReference copyField{get; set;}
    public ControllerClass(){
        Cd = new Candidate__c();
    }
    public PageReference copyField(){
        if(selected1==true){
        Cd.Last_Name__c = Cd.First_Name__c;
        }
        return null;
    }
}

Also, check the below links.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionSupport.htm 

http://www.sfdcpoint.com/salesforce/actionsupport-visualforce-salesforce/

Hope this helps you!
Best Regards,
Deepthi

This was selected as the best answer
SFDCExplorerSFDCExplorer
Hi Ravi and Deepthi

Thank you so much for your reply. I got what I required exactly.