This example expands the query detailed in "JavaScript onPreSave Function Example 1: Checking a Single Condition" by including two further checks, each with a unique message. In this example, the following checks are made:
...
Code Block |
---|
<html><head><title>PreSaveExample2</title><script type="text/javascript"> function fieldsOnPreSave(){ return "TASKDUEDATE,TASKDT02,TASKTXT01,TASKTXT04,TASKDT01"; } function onPreSave(DueDate, LastAssessmentDate, EndTime, StartTime, CompletedDatetime){ if(DueDate > LastAssessmentDate) { if(EndTime > LastAssessmentDate) { if(CompletedDatetime > StartTime) { return true; }else{ window.location = "melt://showAlert:/Completed Date can not be earlier than Start Time"; return false; } }else{ window.location = "melt://showAlert:/End Time cannot be earlier than Assessment date"; return false; } }else{ window.location = "melt://showAlert:/Due Date cannot be before Assessment Date"; return false; } } </script></head><body></Body></html> |
...
In the third condition, the completed field date is checked first to see if it is blank. Because date time fields are numeric, you can check if it is equal to zero (0). The double bar or pipe symbols (||) indicates to include an OR query. So the condition is met if the completed date is blank OR the completed date is greater than the start date.
If the activity is in progress, then the Completed field might not be populated. If you do not include the code completed==0 and a completed date is not entered, then the condition is not met and the record will not be saved.
Custom device messages are displayed, in reverse order, in onPreSave example 2 as follows:
...