Triggers: Difference between revisions

11 bytes added ,  6 October 2016
m
no edit summary
mNo edit summary
mNo edit summary
Line 197: Line 197:
</source>
</source>


This script should be used for the Event '''Create entry''' or '''Update entry''' in the Before Saving phase.
This script should be used for the Event '''Create entry''' or '''Update entry''' in the phase '''Before Saving the Entry'''.


==== Set default values ====
==== Set default values ====
If the standard tools can not be set to the desired value of the field by default, it can be done through a script.
If default values cannot be set in normal fashion, it can be done using a script.


===== Previous value of another field =====
===== Previous value of another field =====
There is a library containing the records of daily walks or use of a car or bicycle. The library has a StartingMileage field and a Mileage field. When an entry is created, the field StartMileage must get data from the field Mileage in the previous entry.
Suppose there is a library containing daily mileage of daily walks or use of a car or bicycle. Suppose the library has a StartingMileage field and a Mileage field. When an entry is created, the field StartMileage must get data from the field Mileage in the previous entry.


<source lang="javascript" line>
<source lang="javascript" line>
var entries = lib().entries();
var entries = lib().entries();                     // Get the current library and an array of its entries
if (entries.length > 0) {
if (entries.length > 0) {                           // Check that the array is not empty; otherwise exit, since there is no previous entry.
prevMileage = entries[0].field("Mileage");
prevMileage = entries[0].field("Mileage");         // The array is sorted from newest to oldest, so the newest entry already in the array will be on top with index of 0.
entryDefault().set("StartMileage" , prevMileage )
entryDefault().set("StartMileage" , prevMileage )   // Set the value of the field Mileage from the previous entry as the default value for the field StartMileage.
}  
}  
</source>
</source>


# Get the current library and an array of its entries.
The script must be set for Event '''Creating an entry''' phase '''Open the Entry Edit card for Add'''.
# Check that the array is not empty of entries; otherwise stop the script, so we do not have a previous entry.
# The array of entries is sorted from newest to oldest. The previous entry is set to the newest entry at the beginning of the array with an index value of 0. Get the Mileage field from the previous entry.
# We set the value of the field Mileage from the previous entry as the default value for the field StartMileage.


The script must be set for action ''Creating and recording phase opening of the card''.
===== Beginning of the next day =====
 
If you need to identify the beginning of a new day in the Date/Time field, the script requires to connect the JavaScript library '''moment.js''' [http://momentjs.com/ moment.js]
===== Beginning the next day =====
If you want the date/time field when creating a record, set the beginning of the next day, it can make the following script. For the script, you must connect the JavaScript-library moment.js [http://momentjs.com/ moment.js]


<source lang="javascript" line>
<source lang="javascript" line>
var m = moment().add(1,'d')
var m = moment().add(1,'d')                       // Using the moment.js library function moment(), get the current time and add 1 day
m.hour(8).minute(0)
m.hour(8).minute(0)                               // Set the time to hour 8 and minute 0
entryDefault().set("Date" , m.toDate().getTime())
entryDefault().set("Date" , m.toDate().getTime()) // Use that as the default value for the field Date
</source>
</source>


# With the moment () function library moment.js obtain the current time and add 1 day.
The script must be set for Event '''Creating an Entry''' and phase '''Opening an Entry Edit card for Add'''.
# Set time of 8 hours and 0 minutes.
# Write a default value for the field Date.
 
The script must be set for action Creating a record and phase Opening a form.


== Working with files ==
== Working with files ==