Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »


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:

  • Is the due date greater than the planned date (as shown in onPreSave example 1)?
  • Is the planned completion date greater than the planned date?
  • Has the completed date been entered and if so, is it greater than the start date?


The following assumptions are made about the field IDs in this example:

  • The Last Assessment Date field ID is TASKTXT01.
  • The Start Time field ID is TASKTXT04.
  • The Completed Date Time field ID is TASKDT01.



The onPreSave example 1 checks only a single condition. The onPreSave example 2 checks multiple conditions.
An example of a complete onPreSave query with header and footer information, which checks multiple conditions, is as follows:

<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> 




where:
function fieldsOnPreSave(){ return "TASKDUEDATE,TASKDT02,TASKTXT01,TASKTXT04,TASKDT01"; } defines all the fields that must be read.
function onPreSave(DueDate, LastAssessmentDate, EndTime, StartTime, CompletedDatetime) defines the names for the fields that must be read.
The multiple conditions are then defined. The structure of the multiple conditions in onPreSave example 2 is as follows:

{if( condition 1 )
{if( condition 2 )
{if( condition 3 )
{return true;} else{ message 3; return false; }
}else{ message 2; return false; }
}else{ message 1; return false; }
} 



If all the conditions are met, then a True value is returned allowing the record to be saved. If the conditions are not met, then a message is displayed indicating which condition was not met, and a False value is returned preventing the record from being saved.
 

 

Notice that return true; is defined first. If all conditions are met, then the code exits and the user can save the record. The other conditions are checked in the reverse order that they are listed, so the messages displayed are defined in the reverse order also. Nested IF arguments are checked in a similar way also.

 

In onPreSave example 2, the code is as follows:

Condition 1 = ( DueDate > LastAssessmentDate )
Condition 2 = ( EndTime > LastAssessmentDate )
Condition 3 = ( CompletedDatetime > StartTime ) 


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:

Message 3 = Due Date cannot be before Assessment Date
Message 2 = End Time cannot be earlier than Assessment date
Message 1 = Completed Date can not be earlier than Start Time 
  • No labels