JavaScript onDelete Function Example 2: Checking Multiple Conditions
This example expands the query detailed in "JavaScript onDelete Function Example 1: Checking a Single Condition"Â by showing how to prevent open activities from being deleted if the parent (Account) is a customer. In this case, deleting records that are open for prospects is allowed, but not for customers. The following assumptions are made in this example:
- The status is stored in the field CUSTKW02.
- The status value that is being checked is Open.
- The parent (Account) module ID is 2.
- The Account Type field in the parent is ACCTKW01.
- The value that is being checked in Account Type is Customer.
An example of a complete onDelete query, which checks multiple conditions, is as follows:
<html><head><title>OnDeleteExample2</title> <script type="text/javascript"> function fieldsOnDelete(){ return "CALTYPE,1.ACCTKW01";} function onDelete(Type,AccountType){ if( Type == 'Call' && AccountType == 'Prospect' ){ window.location = "MELT://showAlert/ You cannot delete open Call Activities from Prospect Accounts"; return false; }else{ return true; } } </script></head><body></body></html>
where:
- function fieldsOnDelete(){ return "CALTYPE"; } is enhanced to include a second field to check for. A comma is inserted first to indicate another field, and as that field is in a parent module, the Module ID of the parent object must be included with a period symbol (.) separating the module ID and field name. The code in onDelete example 2 is as follows:
Â
function fieldsOnDelete(){ return "CALTYPE,1.ACCTKW01"; }
Â
- A name is given to the second fields so function onDelete(status) is enhanced to include the second field called accttype. The code in onDelete example 2 is as follows:
Â
onDelete(Type,AccountType)
Â
- if( Type == 'Call') is enhanced to include the second parameter (does AccountType include Prospect?). A double ampersand (&&) after the first line separates the two parameters. The code in onDelete example 2 is as follows:
Â
if( Type == 'Call' && AccountType == 'Prospect')
The message changes from You cannot delete open Activities in onDelete example 1 to You cannot delete open Activities from Customer Accounts in onDelete example 2.