Event Handlers: fieldsOnEvent and onEvent
The fieldsOnEvent and onEvent handlers use two event handlers in order to process the event. The fieldsOnEvent handler declares the set of data that is to be retrieved and passed in to the onEvent handler. The onEvent handler processes the data and returns the result.
The fieldsOnEvent and onEvent handlers include the following:
- fieldsOnCancel or onCancel
- fieldsOnDelete or onDelete
- fieldsOnPreSave or onPreSave
- fieldsOnSave or onSave
- fieldsOnConditionQuery or onConditionQuery
- fieldsOnFilter or onFilter
- fieldsOnDefault or getDefaultValueOfMEFIELDNAME
In the following example code, the onPreSave function is run using the fieldsOnPreSave function command, and the action is performed before the file is saved. After the function is defined, a return command, which is inside the braces {}, lists the fields that are to be read:
function fieldsOnPreSave(){ return "this.CUSTDT01,this.CUSTDT02"; }
In analyzing the return command inside the braces {}, note the following:
- When reading fields, the return command is always used and the returned fields are included within quotation marks, followed by a semicolon.
- Each field read is separated by a comma.
- Fields read are identified using MODULEID.SERVERFIELDNAME. In this example, the module ID is replaced with "this" because the fields are contained within the current module. But if reading fields from a parent, then the module ID of the parent object would be used. For more information about identifying Module ID and Swift MEAP Field Name, see "Obtaining Module ID and Field Name Information".
- In this example, the CUSTDT01 field stores the start time.
- In this example, the CUSTDT02 field stores the end time.
Upon receipt of the field list defined by the fieldsOnEvent handler, the application looks up the specified field values and calls the onEvent handler with the values passed in as parameters.
In the following example code, fields are not being updated. The onPreSave function is giving the fields a nickname (or alias), which will be used in conditional queries. Since field names might be difficult to remember, giving a field a descriptive name makes it easier when creating code and also ensures that others reading the code will understand the query being performed.
Field names are defined in the order that they are read. In the previous example, the CUSTDT01 (Start Time) field was read first followed by the CUSTDT02 (End Time) field.
In the following example, the fields are given the (alias) names starttime and endtime:
function onPreSave(starttime, endtime)
Thereafter, the onEvent handler is free to process the data as it sees fit. It is important for the onEvent handler to return the value type that is expected by the event handler. This varies by event. Failure to return the correct type will result in errors.
The IF command in the following example indicates to perform one action if a condition is met, and another two actions if the condition is not met.
function onPreSave(starttime, endtime){ if( endtime > starttime ){ return true; }else{ window.location = "melt://showAlert/End Date must be greater than Start Date"); return false; } }
In this example, the end time is checked to see whether it is greater than the start time. If it is, then the record is saved. However, if it not, then two actions are performed as follows:
- A message is displayed to indicate why the record has not been saved.
- The user is returned to the record entry screen without saving the record.
With an IF statement, if the condition is met, then the first action is executed. Returning a True value in an onPreSave conditional query allows you to save a record, so immediately after the conditional query, the action { return true; } is inserted. If the condition is not met, then an else parameter defines what will happen as follows:
If the condition is met return a true value, otherwise do the following. . .
In this example, two actions will be performed if the condition is not met as follows:
- a message is displayed to the user and
- a False value is returned, which prevents a record from being saved in an onPresave conditional query.
Both actions are contained within the same braces {}, and both actions are separated by a semicolon. The melt://showAlert function displays a message within a window, which the user must close, and the function is followed by the False value (return false; ). The event handler is closed with another right brace }.