JavaScript onCreate Function Example 1: Read and Insert
In this example, a new Contact record is created, which has a parent Account. The main phone number and fax number are read from the Account record and inserted into the Telephone and Fax number fields in the Contact record. The user can change the value in the fields before saving if required. In this example, the following assumptions are made:
- The module ID of the parent (Account) is 2.
- The phone number MEFIELDNAME in the parent (Account) is ACCTPHONE01.
- The fax number MEFIELDNAME in the parent (Account) is ACCTFAX01.
- The phone number MEFIELDNAME in the current object (Contact) is CONTPHONE01.
- The fax number MEFIELDNAME in the current object (Contact) is CONTFAX01.
For more information about locating the module IDs and field names, see "Obtaining Module ID and Field Name Information".
The typical structure of an onCreate query, which reads fields and inserts fields from a parent, is as follows:
function fieldsOnDefault(){ return "MEFIELDNAME";} function getDefaultValueOfMEFIELDNAME(){ return "MODULEID.MEFIELDNAME" }
NOTE: The onCreate code is saved in the Module Settings for the Contact module.
An example of a complete onCreate query with header and footer information, which reads fields and inserts fields from a parent, is as follows:
<html><head><title>onCreateExample1</title><script type="text/javascript"> function fieldsOnDefault(){ return "CONTPHONE01,CONTFAX01"; } function getDefaultValueOfCONTPHONE01(){ return "2.ACCTPHONE01" }Â function getDefaultValueOfCONTFAX01(){ return "2.ACCTFAX01" } </script></head><body></body></html>
where:
- fieldsOnDefault() { return "MEFIELDNAME"; } defines the fields to be set. To set the value in the two fields (CONTPHONE01 and CONTFAX01) within the contact, the MEFIELDNAME is replaced with these two values and separated by a comma in onCreate example 1 as follows:
function fieldsOnDefault() { return "CONTPHONE01, CONTFAX01"; }
- function getDefaultValueOfMEFIELDNAME(){ return "MODULEID.MEFIELDNAME" } sets each field. It lists the field to be set in the getDefaultValueOf command and the value to insert (return) from the parent by listing the module ID and field name of the parent field. This is done one field at a time in onCreate example 1 as follows:
function getDefaultValueOfCONTPHONE01(){ return "2.ACCTPHONE01" }Â function getDefaultValueOfCONTFAX01(){ return "2.ACCTFAX01" }