Trigger script

255 views
Skip to first unread message

Graham Wood

unread,
Jan 31, 2023, 4:50:22 PM1/31/23
to mementodatabase
Im not sure where to start with this on.

I have a countdown in my Holiday booking database which works well within the database if I open a record then save it.
Is it possible to make it a trigger script so when the actual library is opened it updates the countdown. 
This would then show the actual countdown days live without going into the library and editing and saving

This is my countdown script, can I just put that script in a trigger and set it to when opening the library or has more got to be added to the script for it to function

if (field("Count")<="0")'0';
else field("Count")

Bill Crews

unread,
Jan 31, 2023, 5:44:03 PM1/31/23
to Graham Wood, mementodatabase
This script is when as a JavaScript field. For a trigger, you'd need something like...

var e = entry();
var count = e.field("Count");

if (count > "0")
else e.set("Count, count);

Now, using "0" to signify today's date is new to me. If that works, then great, because my way would be more complex, as you'll see...

var e = entry();
var today = new Date(new Date().setHours(0, 0, 0, 0))
e.set("Count", ((e.field("TargetDate").sethours(0, 0, 0, 0)
- today) / 86400000).toFixed();

That's off the top of my head. Test it & adjust if needed, if you decide to use it.
--
You received this message because you are subscribed to the Google Groups "mementodatabase" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mementodataba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mementodatabase/6897deb9-35ec-4126-8957-f7ae7124ff65n%40googlegroups.com.

Graham Wood

unread,
Jan 31, 2023, 6:12:42 PM1/31/23
to mementodatabase
thanks Bill, both those options give errors when I run the trigger

Bill Crews

unread,
Jan 31, 2023, 6:23:08 PM1/31/23
to Graham Wood, mementodatabase
Any hints as to the error message, which line it occurs in, or anything else. If not, I guess you're on your own. I assume you're putting this as a trigger on Opening the library. Maybe I left off a final parenthesis just before the semicolon?


Graham Wood

unread,
Jan 31, 2023, 6:49:10 PM1/31/23
to mementodatabase
On the more complex one it says missing ) after argument list js.#6  so at the end of line     e.set("Count", ((e.field("TargetDate").sethours(0, 0, 0, 0) I added ) o n the end,  then it comes back and says TargetDate is not defined

on this version  the last line has a syntax error so I added this " after Count but still syntax error
var e = entry();
var count = e.field("Count");

if (count > "0")
else e.set("Count, count);

Just to clarify the fields in my database so you get more of an understanding how it works just incase I have given incorrect information

My Field called Count is a calculation and is a hidded field, this is what is in it
if(#{startdate}!=0,ceil((#{startdate}-now())/86400),0)-1

Because it returns a negative number after reaching 0 I then have a field called Countdown with this javascript

if (field("Count")<="0")'0';

else field("Count")

so it will only go down to 0 and not show a negative number, see attached image for how Countdown is displayed in the DB

Basically to see that number go down I have to press edit in the DB then it changes, so today it went to 98 after pressing edit and saving.

What I am trying to achieve via a Trigger is to just open the DB and see Countdown go down without having to press the edit button
Hope this makes sense


Screenshot-Coundwown.jpg

Bill Crews

unread,
Jan 31, 2023, 7:15:01 PM1/31/23
to Graham Wood, mementodatabase
Hmmm... There are only 4 statements in the script, so the number 6 is interesting. However, I did leave off a final ) just before the final ;

else e.set("Count, count);

...is wrong because the first argument to set() is to be a string, and the string is unterminated, so add a " there.

But the main script I suggested is the following 4 lines...

var e = entry();
var today = new Date(new Date().setHours(0, 0, 0, 0))
e.set("Count", ((e.field("TargetDate").sethours(0, 0, 0, 0)
- today) / 86400000).toFixed());

...I think I got the ) right this time.

Graham Wood

unread,
Jan 31, 2023, 7:34:37 PM1/31/23
to mementodatabase
it comes back and says TargetDate is not defined. I assume as I dont have a field called Target Date

Bill Crews

unread,
Jan 31, 2023, 7:36:41 PM1/31/23
to Graham Wood, mementodatabase
I'm sorry. I should have said to change that string to whatever field name you're using to represent the upcoming deadline.


Graham Wood

unread,
Feb 2, 2023, 5:57:14 AM2/2/23
to mementodatabase
Bill I got this script to work using this script but if I set the trigger on open library I get this error. See attached 

var e = entry(); 
var days = e.field("Count Days"); 

if (days <="0") {
e.set("Countdown",'0');
} else {
e.set("Countdown",e.field("Count Days"). toFixed ());
}



Screenshot_2023-02-02-21-48-40-28_1a0e309e342d0b39d45ce86487c154fd.jpg

Craig Hunter

unread,
Feb 2, 2023, 8:52:03 AM2/2/23
to Graham Wood, mementodatabase
The trigger Open library  knows which library is opened, but it has access to all the entries in the library. 
var e = entry(); 
This is null. It doesn't know which entry.
There is an object which has a list of all the entries in the library. I do not recall the details right now.
-Craig

Bill Crews

unread,
Feb 2, 2023, 11:27:37 AM2/2/23
to Craig Hunter, Graham Wood, mementodatabase
I haven't done this a lot, but Craig is right. I hadn't thought about it. entry() would return null if the Event were on Opening the library.

But then, though the event and its script are initiated upon the event, the trigger script would have to run like an action script, running through all the entries, and I suppose updating all those Count fields as well. If that's what you want, then here would be the script...

var entries = lib().entries();
for (x in entries) {
    var e = entries[x];

    var days = e.field("Count Days"); 

    if (days <= 0)
        e.set("Countdown", 0);
    else
        e.set("Countdown",e.field("Count Days").toFixed ());
}

As you can see due to my indentation, the final } terminates the for statement block, just to be clear.

I removed the quotes around 0, since you're working with it as a number and not as a string. Also the call to toFixed() must immediately follow the . following the call to field(), so I removed the inappropriate space following the . .

That'll update the count in all entries each time the library is opened.


Craig Hunter

unread,
Feb 2, 2023, 12:58:01 PM2/2/23
to Bill Crews, Graham Wood, mementodatabase
I could be misremembering, but if you just want to access the most recently created entry,
   var e = entries[0];
and no looping needed.

Bill Crews

unread,
Feb 2, 2023, 2:11:51 PM2/2/23
to Craig Hunter, Graham Wood, mementodatabase
Good point, and that's true. So one could start a regular Opening the library trigger script with var e = lib().entries[0]; instead of var e = entry(); and use the rest of the script as usual.

Thanks, Craig.

Er Mo

unread,
Feb 2, 2023, 2:30:56 PM2/2/23
to mementodatabase
Hallo
Da ist mir jetzt einiges Unklar . Wenn ich nur den Letzten Eintrag einer Bibliothek beim Öffnen aktualisire , was ist mit den Anderen . Wenn ich einen Eintrag hinzufüge wird der vorige nicht mehr bearbeitet . Oder Sprechen wir von " Öffnen der Ansichtkarte ". Dann würde ich anstatt entry() einfach entryDefault() verwenden .

Hi
Something is unclear to me now. If I only update the last entry of a library when I open it, what about the others. If I add an entry, the previous one is no longer edited. Or let's talk about " opening the postcard ". Then instead of entry() I would just use entryDefault() .

Ernst

Graham Wood

unread,
Feb 2, 2023, 4:32:14 PM2/2/23
to mementodatabase
thankyou guys your all a wealth of knowledge. I have had Memento DB for about 6 years now and only just decided around 3 weeks ago to use it and start using it with scripts and triggers.
The reason for starting to use it we are travelling a lot more overseas now and its perfect for keeping all required information.
I have a great DB together now that logs all the required info.

Another question, I want to put it up for everyone to have access to it and tweak to their needs  how to I upload an empty DB and it has 2 libraries (Holidays & Trael Locations) do I just upload the main library
and the other will follow or do I need to upload the both
Thankyou once again

Bill Crews

unread,
Feb 2, 2023, 7:00:07 PM2/2/23
to Er Mo, mementodatabase
When you save the "last" created entry, it will still be entries[0] until another entry is added, since the order is by creation date. If you choose to handle only entries[0] on Opening the library, the remaining entries will be unaffected. Once you've added another entry, it is now entries[0], and the previous one is entries[1]. If you have a trigger set on Opening the card and you open the most recent entry, the trigger will fire, but the entry will not be saved until the Save checkmark is pressed. Since opening a card for an existing entry is different from creating a new entry, entryDefault/DefaultEntry are not involved.

I didn't test this, but it is my understanding. Comments, anyone?


Graham Wood

unread,
Feb 13, 2023, 4:54:58 PM2/13/23
to mementodatabase
I have come across a little issue with my trigger script to show the days countdown when opening the library. I will try to expain the issue here
My trigger script is this and the trigger is set to open a library and I have tried setting  the phrase to both Before and after window display and get the same results. It wont update

var entries = lib().entries();
for (x in entries) {
var e = entries[x];
var days = e.field("Get Days");

if (days <= 0)
e.set("Countdown", 0);
else
e.set("Countdown",e.field("Get Days"));
}

what I'm finding is when I open the library the countdown does not change.
So to get around it I made another trigger script and set it to Open a Library and the phrase is Before Window Display
Here is the modified script and it basically removes any data from the countdown field.

var entries = lib().entries();
for (x in entries) {
var e = entries[x];
var days = e.field("Get Days");

if (days <= 0)
e.set("Countdown", 0);
else
e.set("Countdown", ' ');
}

I then have my original trigger script and set it to Open a Library and the phrase is After Window Display
I then open the library and tap on my current entry called Bali May 2023 to show the data in it and it then re populates the countdown field
and displays correct. Here is the script for this

var entries = lib().entries();
for (x in entries) {
var e = entries[x];
var days = e.field("Get Days");

if (days <= 0)
e.set("Countdown", 0);
else
e.set("Countdown",e.field("Get Days"));
}

using the 2 trigger scripts in this fashion is working fine but Im just wondering if there is a way to combine both scripts together so that when I open the library
it clears the countdown field first then puts the value back in automatically from the Get Days field.

The order I currently have the trigger scripts in is the one with Before Window Display is first in the list followed by the After Window Display script.
Do the scripts execute in the order they are placed?

hopefully I have explained this so you guys can understand what is going on.

Bill Crews

unread,
Feb 13, 2023, 7:23:08 PM2/13/23
to Graham Wood, mementodatabase
I'm not sure about the phases, but I note that you never reduce the value of the Countdown field. Isn't it supposed to count down? Is Get Days a Date field?


Graham Wood

unread,
Feb 13, 2023, 7:44:45 PM2/13/23
to mementodatabase

Bill Get Days is just a calculation field that has this in it
(#{from date}-now())/86400

From date is the date the holiday period starts from and in this case is 11th May 2023

Graham Wood

unread,
Feb 13, 2023, 8:10:36 PM2/13/23
to mementodatabase

I have separated my triggers now Bill so trigger 1 now is this. Basically im resetting the countdown field to nothing so the other trigger will re populate it with the correct amount of days left.
I then told my 2nd trigger to  Open a Library and show Before Window Display, this seems to be ok for now. I will see how it goes tomorrow when I open it again to see if it has counted down correctly
As you can see from image 1 attached it is showing 85 days and also image 2
Originally when I opened the library image 1 would not show 85 days but when I tapped on it to open it showed correct like in image 2 then image 1 would show correct again till it was opened again then it would be blank.
Hopefully by resetting the countdown field first then opening the library it will show how I want it, its not a big issue at all just wanted to reduce steps in tapping on phone.

var entries = lib().entries();
for (x in entries) {
var e = entries[x];
var days = e.field("Countdown");

if (days <= 0)
e.set("Countdown", 0);
else
e.set("Countdown", ' ');
}
1.jpg
2.jpg

Bill Crews

unread,
Feb 13, 2023, 10:21:08 PM2/13/23
to Graham Wood, mementodatabase
Got it. I hope you figure out how to do it in one script/trigger/phase, and I hope you teach us what you learn. Good luck.

Graham Wood

unread,
Feb 16, 2023, 4:40:54 PM2/16/23
to mementodatabase
Hi Bill,

I have finally got this all working as I wanted to so when the library is opened the countdown is updated to show correctly.
I removed all my trigger scripts and existing Get Days field  that was populating a Field called Countdown (integar field)

I then settled on 1 field only and its a calculation field (Countdown) set to integar and here is the calculation I placed in it

if(#{from date}-now()<=0,0,#{from date}-now()).toFixed()/86400

now when the library is opened it shows instantly without any other scripting controlling it.

I may have been overthinking this from the start on how I wanted it to work and it was just a process of failures to push me to look for another solution

Bill Crews

unread,
Feb 17, 2023, 2:17:17 AM2/17/23
to Graham Wood, mementodatabase
If anyone is interested in the JavaScript equivalent of your Calculation script, here it is...

var remaining = field("Deadline") - Date.now();
if (remaining <= 0)
    "";
 else

    (remaining / 86400000).toFixed();

I'm surprised toFixed() is supported in Calculation script. I thought that was a JavaScript-only method for numbers. In JavaScript, times are in milliseconds. I guess in Calculation scripts, they are in seconds. I have a JavaScript EntryColor field to color expired countdowns differently. Later, I'll add code to advance the deadline by a year or other unit of time, so I don't have to keep adding recurring deadlines.


Reply all
Reply to author
Forward
0 new messages