Welcome to MSDN Blogs Sign in | Join | Help

Fun with JScript: Copying Ship To Address to Bill To Address

Do you have Billing and Shipping Addresses on any of your forms? Do you have customers that keep asking if there is a way to click a button and copy the address from one to the other if they are the same? Well, here is a sample piece of code I built that will do just that. This assumes that you are making the Billing Address the same as the Shipping Address, but you can modify it to fit any scenario and adjust the fields included as needed.

Thanks to David Pritchett from ePartners to providing me with this code for the blog.

1)       Create an attribute (I used a bit –Yes/No). Call it something like “Billing Same As Shipping.”

2)       Put this attribute on your form where the addresses are.

3)       Add the following code to the OnChange event of this new attribute and enable:

 /* Declare your variables to store the data from the Address fields on the form so we can reference them later */

var osaddress1_line1 = document.crmForm.all.address1_line1.DataValue;

var osaddress1_line2 = document.crmForm.all.address1_line2.DataValue;

var osaddress1_line3 = document.crmForm.all.address1_line3.DataValue;

var osaddress1_city = document.crmForm.all.address1_city.DataValue;

var osaddress1_stateorprovince = document.crmForm.all.address1_stateorprovince.DataValue;

var osaddress1_county = document.crmForm.all.address1_county.DataValue;

var osaddress1_postalcode = document.crmForm.all.address1_postalcode.DataValue;

var osaddress1_country = document.crmForm.all.address1_country.DataValue;

var osaddress1_fax = document.crmForm.all.address1_fax.DataValue;

var osaddress1_telephone = document.crmForm.all.address1_telephone1.DataValue;

 

/* Declare your variable to capture the data from the attribute being changed – Billing Same As Shipping in this case */

var oField = event.srcElement;

 

/* If the value of the Billing Same As Shipping field you captured is 1 (Yes), then update all of the corresponding Billing Address fields with the data from the Address fields */

if (oField.DataValue == 1)

{

document.crmForm.all.address2_line1.DataValue = osaddress1_line1;

document.crmForm.all.address2_line2.DataValue = osaddress1_line2;

document.crmForm.all.address2_line3.DataValue = osaddress1_line3;

document.crmForm.all.address2_city.DataValue= osaddress1_city;

document.crmForm.all.address2_stateorprovince.DataValue = osaddress1_stateorprovince;

document.crmForm.all.address2_county.DataValue = osaddress1_county;

document.crmForm.all.address2_postalcode.DataValue = osaddress1_postalcode;

document.crmForm.all.address2_country.DataValue = osaddress1_country;

document.crmForm.all.address2_fax.DataValue = osaddress1_fax;

document.crmForm.all.address2_telephone1.DataValue = osaddress1_telephone;

}

 

There you have it! All of the data from the Shipping Address fields will be copied to the Billing Address fields when you select the “Yes” option on your new attribute.

Enjoy!

Published Monday, May 15, 2006 7:00 AM by Ben Vollmer

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Fun with JScript: Copying Ship To Address to Bill To Address

You might want to use the W3 standard document.getElementById instead of the IE proprietary document.all function.

if (oField.DataValue == 1) {
document.getElementById('address2_line1').value = document.getElementById('address1_line1').value;
}

Personally I'd write a small 3 line function to  copy all values from anything beginning with one id string to another.

[)amien
Monday, May 15, 2006 7:39 AM by Damien Guard

# re: Fun with JScript: Copying Ship To Address to Bill To Address

That would certainly work, however, document.crmForm.all.address2_line1.DataValue is the documented (and Microsoft supported) way of performing the task. Given that CRM is designed to only run under Internet Explorer, using IE-proprietary functions isn't an issue.
Tuesday, May 16, 2006 4:51 PM by Keith

# re: Fun with JScript: Copying Ship To Address to Bill To Address

How does this map say going to GP via the GP to CRM integration. The reason I ask this is that I built the integration between these 2 for my client, out of the box the ship to address is populated in 'Addresses' rather than on the General tab as Address2? Any ideas of the ramifications of using this method and would it populate GP properly, I haev a feeling we would need a component to map this to GP correctly?

Thanks,
Scott
Monday, June 26, 2006 1:45 PM by Scott

# re: Fun with JScript: Copying Ship To Address to Bill To Address

hi

Is this possible to do a similar sort of thing but instead copy values from fields in one entity to a field in another entity.
Regards
Ridhima
Monday, September 11, 2006 10:47 PM by Ridhima

# re: Fun with JScript: Copying Ship To Address to Bill To Address

Yes. It is called Mappings. :-)
Monday, September 11, 2006 10:49 PM by Ben Vollmer

# re: Fun with JScript: Copying Ship To Address to Bill To Address

hi Ben

Thanks for the reply. I have a field called device name in contact but i am unable to map it to a new field called device in service activity. I can not edit the default relationship so was wondering if there is some sort of code that i can use to populate the device name in service activity when a particular customer is selected.

regards
Ridhima
Tuesday, September 12, 2006 6:42 PM by Ridhima

# re: Fun with JScript: Copying Ship To Address to Bill To Address

Hi ,

I am having trouble in getting a Auto Number to generate in the leads page . I have added one more field as Lead ID and I want an Auto Number to geneate. Logically what should happen is to get the last record of the database and then add one to it . Is there a java script that can be used for this or is there a reference that I can do to the attributes like  "crmForm.all"

Thanks

Sahan

Wednesday, June 18, 2008 9:32 PM by Sahan

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker