/
JavaScript onPreSave Function Example 3: Checking a Parent
JavaScript onPreSave Function Example 3: Checking a Parent
A quotation is entered in this example. The quotation has a parent Account, which contains a Customer Type keyword to indicate whether the customer is a preferred or a standard customer. Users are allowed to enter a discount if the customer is a preferred customer only. To perform this query, this example makes the following assumptions:
- The parent account MODULEID is 2.
- The MEFIELDNAME for the Customer Type field in the parent account is ACCTKW05.
- The possible values in Customer Type keyword are Preferred or Standard.
- The MEFIELDNAME for the Discount field is CUSTTXT07.
The query in this example is based on the code used in onPreSave example 1:
<html><head><title>PreSaveExample1</title><script type="text/javascript"> function fieldsOnPreSave(){ return "TASKDT01,TASKDT02"; } function onPreSave(due,planned){ if( due > planned ) { return true; }else{ window.location = "melt://showAlert/$due.greater.than.planned"; return false; } } </script></head><body></body></html>
The complete code for onPreSave example 3 with header and footer information is as follows:
<html><head><title>PreSaveExample3</title><script type="text/javascript"> function fieldsOnPreSave(){ return "CUSTTXT07,2.ACCTKW05" } function onPreSave(discount,custtype){ if(discount==0 || custtype=='Preferred'){ return true; }else{ window.location = "melt://showAlert/You can only enter a discount for Preferred customers"; return false; } } </script></head><body></body></html>
For onPreSave example 3, the following changes are made to onPreSave example 1 code:
- The fieldsOnPreSave function is changed to read the field in the current object and the field from the parent as follows: "CUSTTXT07,2.ACCTKW05"
- Different names are used for the fields by changing the onPreSave function to the following: onPreSave(discount,custtype)
- Two checks are performed: to return a true value if the discount is equal to zero (discount == 0), and to return a true value if the Customer Type is Preferred (custtype=='Preferred') since preferred customers are allowed a discount. Both queries are combined by using an OR query with a double bar or pipe symbols (||) as follows: { if( discount==0 || custtype=='Preferred') { return true ;}
- The message that is displayed when the condition is not met is changed, and a False value is returned preventing the record from being saved as follows:
window.location = "melt://showAlert/You can only enter a discount for Preferred customers"; return false; }