JavaScript onOpen Function Example: Read and Insert


This example shows you how to add an Order Item using the quick selector list. To speed up data entry, the order number is read from the parent Order and inserted into the order number field in the Order Item record. You can change the values in these fields if required before saving. In this example, the following assumptions are made:

  • The module ID of the parent Order is 9.
  • The order number MEFIELDNAME in the parent order is ORDERTXT01.
  • The order number MEFIELDNAME in the current order item object is CUSTTXT02.



For more information about locating module IDs and field names, see "Obtaining Module ID and Field Name Information".
The typical structure of an onOpen query, which inserts fields from a parent object, is as follows:

function fieldsOnDefault(){ return "MEFIELDNAME";}

function getDefaultValueOfMEFIELDNAME(){ return "MODULEID.MEFIELDNAME" } 

The onOpen code is saved in the Module Settings of the Order Item module. An example of a complete onOpen query with header and footer information, which inserts fields from a parent object, is as follows: 

<html><head><title>onOpenExample</title><script type="text/javascript"> 
function fieldsOnDefault(){ return "CUSTTXT02"; } 
function getDefaultValueOfORDERTXT01(){ return "9.ORDERTXT01"; }
</script></head><body></body></html> 


where:

  • fieldsOnDefault() { return "MEFIELDNAME"; } defines the fields to be set. In this example, the value of CUSTTXT02 is being set within the Order Item so MEFIELDNAME must be replaced with this value:

 

function fieldsOnDefault() { return "CUSTTXT02"; }
  • function getDefaultValueOfMEFIELDNAME(){ return "MODULEID.MEFIELDNAME" } sets each field. Each field to set is listed in the getDefaultVaulueOf command, and the value to insert (return) from the parent is specified using the module ID and field name of the parent field. The code for the field in the onOpen example is as follows:

 

function getDefaultValueOfORDERTXT01(){ return "9.ORDERTXT01" }