Author - problem with trigger

191 views
Skip to first unread message

Tomasz K.

unread,
Nov 1, 2017, 5:34:04 PM11/1/17
to mementodatabase
Hello
Sorry but my English is not good.
I have a problem with "trigger"
There is no option in the "memento" for the information who edited the entry.
I need to know that.
I added a field in the database (Author) and "Modification date"
Written trigger:
Updating an entry
Before saving the entry

var e = entry ();
e.set ("Author", e.author);
e.set ("Modification date", new Date ());

It was simple ... but the user's login was saved in the "Author" field.
I would like to have, "Name and Surname".
login A = Name and Surname A
login B = Name and Surname B
e.t.c...
And I have a problem with that.
Will there be someone willing to help me with this problem

Bill Crews

unread,
Nov 1, 2017, 6:13:54 PM11/1/17
to mementodatabase
You need some a priori information about the account, but i know of no Account object, though there may be one. What we have is a Library object, with only one property, title, and an Entry object with only the following properties...

name - entry name
title - entry name
description - entry description
favorites - true, if the entry is in Favorites
deleted - true, if the entry is deleted (it is in the Recycle Bin)
author - the id of the user who created the entry
creationTime - date & time the entry was created
lastModifiedTime - date & time the entry was last modified

While other info could be made available in these properties, this is all the info as of now. I suggest you request it from the developer, best by using Memento.UserVoice.com.

Tomasz K.

unread,
Nov 2, 2017, 4:55:30 PM11/2/17
to mementodatabase
Thanks, Bill.

Tomasz K.

unread,
Dec 27, 2017, 6:27:17 PM12/27/17
to mementodatabase
I had some problems with the base but now I would like to come back to this topic.
Maybe I explained it wrong ...
I have two scripts.

The first one saves the author's login and the date of creating the entry.

Creating entry.
Before saving entry.

var e = entry ();

e
.set ("Autor", e.author);
e
.set ("DATA", new Date ());


The second one is to download data from the "Author" field and save the Name and Surname in the next field.

Creating entry.
After saving entry.

var e = entry();
var autor = e.field("AUTOR");
var podpis = e.field("PODPIS");
       
var loginA = ('Name and Surname A');
       
var loginB = ('Name and Surname B');
       
var loginC = ('Name and Surname C');
var entries = lib().entries();
for (var ent = 0; ent < entries.length; ent++) {
if (entries[ent].field("AUTOR") === autor);
{
if (autor == loginA); {
e
.set("PODPIS", loginA);
cancel
();
}
if (autor == loginB); {
e
.set("PODPIS", loginB);
cancel
();
}
if (autor == loginC); {
e
.set("PODPIS", loginC);
cancel
();
}
message
(podpis);   //debug
break;
}
}



Theoretically, the trigger works but not as I would like. Saves the last name from the last part of the script.
When I modify the script and add "else", I have a script error.

Any ideas?

Bill Crews

unread,
Dec 28, 2017, 8:38:32 AM12/28/17
to Tomasz K., mementodatabase
       var loginA = ('Name and Surname A');

I didn't take time to look it up, but I think the parentheses are just grouping symbols here, so they are extraneousand could be eliminated, but other than being distracting, they should work fine.

if (entries[ent].field("AUTOR") === autor);

Using === instead of == means that the types must also be equal. That usually just means JavaScript will not automatically convert numbers to strings, but since both autor and the field values are strings, this should work fine.

if (autor == loginA); {

This one IS a problem, and each of the other if statements have it, also. The semicolon terminates the statement prematurely and should be omitted. An if statement has a test section followed by an execution section. The semicolon stops things before getting to the execution section, which in your case starts with the open brace. Just delete the semicolon; the statement will end with the close brace.

if (autor == loginB) {

This will work, but if the test for LoginA above succeeded, there is no reason to do this test, so the script will run faster if you add else to it: "else if (autor == LoginB) {". The same with the following if statement.

Tomasz K.

unread,
Dec 28, 2017, 12:46:05 PM12/28/17
to mementodatabase
Script before changes:
The script test gives the result: succeeded. Turns loginA into "Name and Surname A" but after a moment turns into the last "Name and Surname C". As if he did not stop after finding the correct login and executes the last command before breake;
Script after changes:
"if (author == loginB) {" - .Test of the script gives the result: succeeded.
But he does not change the login as if he did not see the command "e.set (" PODPIS", loginA".
Command: "else if (author == loginB) {- behaves the same as" if (author == loginB) {"

So, using a semicolon is not an error here because the script then executes the next command. I do not know why, after finding a login and a change, it does not cease to execute the script.

Bill Crews

unread,
Dec 28, 2017, 1:19:41 PM12/28/17
to Tomasz K., mementodatabase
The semicolon where you had it is always wrong.

It would be helpful to see the script.

Should generally be of the form...

if (a == b) { statement; }
else if (a == c) { statement; }
else if (a == d) { statement; }

Tomasz K.

unread,
Dec 28, 2017, 6:40:58 PM12/28/17
to mementodatabase
You're right.
I changed the script according to your suggestions but it still does not work. I do not know why, since the test gives the correct result. Maybe the loop is incorrect. Or maybe I'm too stupid. ;)

Tomasz K.

unread,
Apr 22, 2018, 9:35:33 AM4/22/18
to mementodatabase

Hello

I had a hard start to the year. Only now I have time to describe the solution.

Thank you Bill, for the help you showed me. I could not do it without you.

Solution 1:

Creating an entry:
If you are interested only in information about who is the author of the new entry, then this trigger will suffice:

// Script adding information who is the author of the new entry

var e = entry ();
if (e.author == "loginA") {
   e
.set("AUTHOR", "userNameA");
}else if(e.author == "loginB") {
   e
.set("AUTHOR", "userNameB");
}else if (e.author == "loginC") {
   e
.set("AUTHOR", "userNameC");
} else {
cancel
();
}



The problem is getting information about who and when modified the entry.
With this I managed to rebuild the trigger.

This trigger creates two hidden files in the phone's memory:
login.txt (for the needs of this trigger is unnecessary but who knows what else to use it)
signature.txt
Each time you create a new entry, these files are overwritten. You just need to remember that each user adds some test entry.

Solution 2:
Trigger 1:
Creating an entry
Before saving the entry

// Add a hidden login.txt file in the memento directory
var e = entry();
var login = e.author;
{
var fLogin = file ("/sdcard/memento/.login.txt");
 fLogin
.writeLine(login);
 fLogin
.close();
var l = fLogin.readLines();
}
// Add the hidden file signature.txt in the memento directory
{
var fSignature = file("/sdcard/memento/.signature.txt");
{
if(login =='loginA') {
 fSignature
.writeLine("userNameA");
 fSignature
.close();
}else if(login == 'loginB ') {
 fSignature
.writeLine("userNameB");
 fSignature
.close();
}else if(login =='loginC') {
 fSignature
.writeLine("userNameC");
 fSignature
.close();
}else{
 cancel
();
}
// Add information: who and when modified the entry
var p = fSignature.readLines ();
 e
.set("SIGNATURE", p);
 e
.set("DATE", new Date ());
 message
('ENTRY ADDED'); // message about adding an entry
}
}



Trigger 2
Updating an entry
Before saving the entry

var e = entry ();
var fSignature = file("/sdcard/memento/.signature.txt");
var p = fSignature.readLines();
 e
.set("SIGNATURE", p);
 e
.set("DATE", new Date ());
 message
('UPDATED ENTRY'); // message about updating the entry



It works for me :)

Bill Crews

unread,
Apr 22, 2018, 11:42:13 AM4/22/18
to Tomasz K., mementodatabase
Excellent, Tomasz! I always enjoy it when people post their solutions to the forum. I confess that I sometimes use them to update the wiki.

Just one comment. In Solution 2 Trigger 1, you have the following line...

var l = fLogin.readLines();

First, fLogin was closed on the previous line, so I'm not sure if or how it works. Second, it is not used later in the script, so I think it is a holdover from something you previously had in there and removed. If I'm right, any user should also remove this line.

The only harm would be a possible error message or if it actually works, slowing things down by loading the text file into an array.

Thanks, Tomasz.

--
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 mementodatabase+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tomasz K.

unread,
Apr 22, 2018, 1:44:32 PM4/22/18
to mementodatabase
You're right. I missed it.
This line is to be deleted.
Thanks Bill.

Updated trigger:

// Add a hidden login.txt file in the memento directory
var e = entry();
var login = e.author;
{
var fLogin = file("/sdcard/memento/.login.txt");
 fLogin
.writeLine(login);
 fLogin
.close();
}
Reply all
Reply to author
Forward
0 new messages