JavaScript onCreate Function Example 2: Conditional Insert
In this example, a conditional query determines which fields are entered into a child object. A Quote module is already set up, and the user wants to add a child Call record. There is a Sales Stage keyword within the Quote object, which defines whether the quotation is Tentative, Confirmed or Ordered. When the call details are entered, the Subject field relates to the status of the quote. If the quote's Sales Stage is confirmed, then the Call Subject field will state something similar to the following: Call regarding Confirmed Quotation. In this example, the following assumptions are made:
- The MODULEID of the parent object (Quote) is 4.
- The FIELDID of the parent field (Quote Sales Stage) is CUSTKW03.
- The Keyword values of the parent field is Tentative / Confirmed / Ordered.
- The FIELDID of the child field (Call Subject) is CUSTTXT02.
The typical structure of an onCreate query, which uses a conditional insert, is as follows:
function fieldsOnDefault(){ return "MEFIELDNAME-MODULEID.MEFIELDNAME"; } function getDefaultValueOfMEFIELDNAME(value){ if( value == '1'){ return "a"; }else if( value == '2'){ return "b"; }else if( value == '3'){ return "c"; }else{ return ""; } }
An example of a complete onCreate query with header and footer information, which uses a conditional insert, is as follows:
<html><head><title>onCreateExample2</title><script type="text/javascript"> function fieldsOnDefault(){ return "CUSTTXT02-4.CUSTKW03"; } function getDefaultValueOfCUSTTXT02(stage){ if( stage == 'Tentative'){ return "Call regarding Tentative Quotation"; }else if( stage == 'Confirmed'){ return "Call regarding Confirmed Quotation"; }else if( stage == 'Ordered'){ return "Call regarding Order Confirmation"; }else{ return ""; } } </script></head><body></Body></html>
where:
- function fieldsOnDefault(){ return "MEFIELDNAME-MODULEID.FIELDID"; } reads the fields that are used in the conditional query. When defining a conditional query, fieldsOnDefault sets the field to be updated and the field used to calculate the field being set. The first field listed (MEFIELDNAME) is the name of the field to be updated. The minus sign is inserted to identify that the field being updated will be calculated using the value stored in another field.
- MODULEID.MEFIELDNAME is the name of the field being read to perform the calculation. In this example, the values "CUSTTXT02-4.CUSTKW03" are inserted. The code used in onCreate example 2 is therefore as follows:
function fieldsOnDefault(){ return "CUSTTXT02-4.CUSTKW03"; }
- function getDefaultValueOfMEFIELDNAME(value) specifies to update a field and gives a nickname to the parent field being read. Because MEFIELDNAME is being replaced with CUSTTXT02 (which is the field being written to), 4.CUSTKW03 is given a nickname to use in the conditional query. The nickname is defined inside the brackets. The (value) variable is replaced with (stage) so that the function used in onCreate example 2 is as follows:
function getDefaultValueOfCUSTTXT02(stage)
- The IF query starts with a left brace { and closes with a right brace }. Between the braces, the name of the field being checked changes from value to stage, the (1,2,3) values being looked for are replaced with (Tentative,Confirmed,Ordered), and the returned (a,b,c) values are replaced with the message to display. Each query is separated by an else if command to check each value in turn. A final else command specifies that if none of the previous conditions are met, then insert a blank value. The conditional query used in onCreate example 2 is as follows:
{ if( stage == 'Tentative'){ return "Call regarding Tentative Quotation"; } else if( stage == 'Confirmed'){ return "Call regarding Confirmed Quotation"; } else if( stage == 'Ordered'){ return "Call regarding Order Confirmation"; } else{ return ""; } }