Write, Bureaucrats, Administrators
4,332
edits
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 | 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 | 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 ===== | ||
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> | ||
The script must be set for Event '''Creating an entry''' phase '''Open the Entry Edit card for Add'''. | |||
===== 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 | |||
<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> | ||
The script must be set for Event '''Creating an Entry''' and phase '''Opening an Entry Edit card for Add'''. | |||
The script must be set for | |||
== Working with files == | == Working with files == |