JavaScript onDelete Function Example 1: Checking a Single Condition


This example shows you how to prevent an activity from being deleted when the activity status is set to open. A message is displayed to users indicating why the activity cannot be deleted. In this example, note the following:

  • Only a single field is queried and that field is within the object that is not being deleted, so it is not necessary to get the parent module ID (MODULEID) or to obtain the status field ID (MEFIELDNAME). For more information, see "Obtaining Module ID and Field Name Information"
  • The status is a Keyword, so the Keyword Item Alias value must be obtained. Keyword and Keyword Item Alias are used for all queries. For more information, see "Using Keywords in Queries".
  • MEFIELDNAME is equal to CUSTKW02.
  • The Keyword Item Alias that is being checked is equal to Open.


The typical structure of an onDelete query, which checks a single condition, is as follows:

function fieldsOnDelete(){ return "MEFIELDNAME"; }
function onDelete(field1){
	if( field1 == 'field value'){
		window.location = "MELT://showAlert/Message to display"; return false;
	}else{
		return true;
	}
} 


An example of a complete onDelete query with header and footer information, which checks a single condition, is as follows:

<html><head><title>OnDeleteExample1</title>
<script type="text/javascript"> 
function fieldsOnDelete(){ return "CUSTKW02"; }
function onDelete(status){
	if( status == 'Open'){
		window.location = "MELT://showAlert/You cannot delete open Activities"; return false;
	}else{
		return true;
	}
}
</script></head><body></body></html> 


where:
function fieldsOnDelete(){ return "MEFIELDNAME";} defines which field to read. To read the value stored in the CUSTKW02 field, the following is defined in onDelete example 1:

fieldsOnDelete(){ return "CUSTKW02";}


function onDelete(field1) indicates to run a query against the field that is just read, and allows you to name the field by inserting a value inside the brackets. You can use the actual field name (CUSTKW02) or something more memorable like status. The code in onDelete example 1 is as follows:

function onDelete(status) 


The conditional query begins with a left brace { followed by the if (field1 == 'field value') condition to define what to check. To check whether the status field that was just named contains the value Open, the code in onDelete example 1 is as follows:

if (status == 'Open') 


{window.location = "MELT://showAlert/Message to display"; return false;} defines what happens if the previous condition is met using the code. According to the code, a message will be displayed and a False value will be returned preventing the record from being deleted. To define which message to display, the code in onDelete example 1 is as follows:

{window.location = "MELT://showAlert/You cannot delete open Activities"; return false;} 


When displaying messages, rather than embedding the message in the code, you can use a device message. For more information, see "Custom Device Messages and Multiple Language Installations".
A right brace } defines the end of the first action. Then an else parameter determines what happens if the conditional query is not met. In this case, returning a True value allows record deletion. The code in onDelete example 1 is as follows:

else{ return true; }