Copying Values

I am still a novice with JavaScript so this will appear elementary to most readers. Following are two JavaScript functions that can be used in Apex to copy, firstly an address and secondly a column in a tabular form.

1. Addresses. It is often the case in a Contact Management database that the same address may need to be entered more than once on the same page. E.g the main address maybe followed by a postal address, work address, next of kin address etc, which may all be the same. An easy way to save the user having to retype the addresses is to use JavaScript to copy the fields, as follows:

Add the following code to the HTML Header section of the page:

function copyField (fromField, toField){
$x(toField).value = $x(fromField).value;
}

function copyAddress(fromAddress1, toAddress1, fromAddress2, toAddress2, fromAddress3, toAddress3, fromCounty, toCounty,fromPostcode, toPostcode){
copyField(fromAddress1, toAddress1);
copyField(fromAddress2, toAddress2);
copyField(fromAddress3, toAddress3);
copyField(fromCounty, toCounty);
copyField(fromPostcode, toPostcode);
}

add a checkbox with the following in the HTML Form Element Attributes:

onclick="copyAddress('P1_ADD1','P1_POSTAL_ADD1','P1_ADD2','P1_POSTAL_ADD2',etc)"

OR add this to the href of an image

href="javascript:copyAddress('P1_ADD1','P1_POSTAL_ADD1','P1_ADD2','P1_POSTAL_ADD2',etc);"

Where main address fields are:
P1_ADDRESS_1
P1_ADDRESS_2
P1_ADDRESS_3
P1_COUNTY
P1_POSTCODE


and Postal address fields are:
P1_POSTAL_ADD1
P1_POSTAL_ADD2
P1_POSTAL_ADD3
P1_POSTAL_COUNTY
P1_POSTAL_POSTCODE


2. Copy the column of a tabular form. I recently had to do this in a financial system that had ledger sheets where the values to be submitted in a column were often, but not always, the values in one of the other columns. Using the same principle as above, the trick is to ensure that each field in the tabular form has a unique ID. This can be done by adding the ROWNUM to the array number in the ID value of the APEX_ITEM API. e.g.

apex_item.text(3, NULL, 4,'','style="text-align:right"','f03_#ROWNUM#')

The copy function then changes to loop through the rows of the tabular form as follows:

function copyFields(fromField1,toField1) {
var cnt=1;
while (document.getElementById(fromField1+cnt) != null){
copyField(fromField1+cnt,toField1+cnt);
cnt+=1;}
}


Then use an 'on event' or url to run the function:

="copyFields('f05_','f03_')"
or
href="javascript:copyFields('f05_','f03_')"

Comments

Patrick Wolf said…
Hi Simon,

are you aware of the APEX javascript functions $s and $v?

Instead of $x(toField).value you can use $s to set a value and $v to read a value.

The advantage is that these APEX javascript functions are aware of the page item type, because not all item types will return a correct value if you read/set them with $x('xxx').value

Regards
Patrick
shunt said…
Hi Patrick, many thanks for the tip; I really need to do more JS work. Does the $s and $v function work with apex_item items or just standard page items? Si
Patrick Wolf said…
I think it was basically build for page items, but I think it will work for tabular form columns as well. But I haven't really tried.