+ Start a Discussion
ethan huntethan hunt 

Trigger To Copy address from one field to another

Hi,

I have two text field A & B and one checkbox. I want when i click the check box address from A will get copy to B before saving.
Could you please suggest what should i do. Weather a trigger, Workflow or formula field.

Regards
Ajay_SFDCAjay_SFDC
Hi  ,
I think Trigger is the best option to acheive this task .

You can write trigger on before insert event of your object .

// For example : Suppose your object is Account

if(trigger.isInsert && trigger.isBefore)
{
   for( Account objacc : Trigger.New)
    {
        if(objacc.chkboxfield == true)
          {
             if(objacc.fieldA !=null)
             objacc.fieldB = objacc.fieldA ;
           }
      }
}
harsha vardhanharsha vardhan
The trigger logic works great. But Siddharth, may I know, whether you need to do this in a custom visualforce page/Standard record edit page?

Because, I guess that you are expecting this functionality to happen before the save click and right after the checkbox is ticked, am I right?

If I am right, you can not have this before save click in standard record edit page(and the trigger is fired after the save button is clicked) but is possible if this is a visual force page.

Regards
-Harsha.
SRajesh8SRajesh8
Hi,

Trigger will not work because trigger will execute when ever dml operaion done.

This can be done by writing method and pass all the fields from and to then pass that method to that checkbox.

OR

By Javascript you can copy before saving i.e just on click the checkbox copy the fields values from one fields to another fields.

Regards,
Rajesh.
ethan huntethan hunt
Hi SRSB,

I am using the folloiwng javascript function but it is not working.Kindly check if something is missing

<apex:page standardController="Invoice__c" >
<apex:form >
<script>
function FillBilling()
{
    var cb = document.getElementById('ic1').value;
    var tb1 = document.getElementById('it1').value;
    var tb2 = document.getElementById('it2').value;
    if (cb.checked)
        {
            tb2 = tb1;
        }
        else
        {
            tb2 = '';
        }
}
</script>

  <apex:inputText value="{!Invoice__c.Billing_Address__c}" id="it1"/> <br/>
  <apex:inputCheckbox value="{!Invoice__c.Copy_Mailing_Address__c}" id="ic1" onclick="FillBilling()"/> <br/>
  <apex:inputText value="{!Invoice__c.Mailing_Address__c}" id="it2" />
 
  </apex:form>
</apex:page>

Regards
harsha vardhanharsha vardhan
Hi Siddharth, 

One can not access the visualforce components directly in javascript as below.

var cb = document.getElementById('ic1').value;
var tb1 = document.getElementById('it1').value;
var tb2 = document.getElementById('it2').value;


Instead, have to use it as below:

var cb = document.getElementById('{!$Component.ic1}').value;
var tb1 = document.getElementById('
{!$Component.it1}').value;
var tb2 = document.getElementById('
{!$Component.it2}').value;

Refer http://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_global_component.htm to better know.

Regards,
Harsha