I want to automatically fill a field of linked library with the name entry of it´s parent library.

1,372 views
Skip to first unread message

Pedro Mapo

unread,
Nov 18, 2016, 9:50:36 AM11/18/16
to mementodatabase
I want to automatically fill a field of linked library with the name entry of it´s parent library. This could be as an example for many other type of fields, not only for the entry name.
I have two libraries: A and B; with fields: 
a0, a1, a2, ..., ai, ... and 
b0, b1, b2, b3, .... 
Where the field 'ai' is link to entry in library B.

In my example:
a0 = integer, auto increment, required and the entry name of library A.
a1 = date field.
a2 = text field.
a3 = link to entry in library B.

b0 = integer, auto increment, required and the entry name of library B.
b1 = integer. This is the field I want to be automatically filled with a0 from it´s parent library A.
b2 = text field.
...

Now, I´m creating a new entry in library A.

I´ve just filled the relevant fields.

I´m in front of field a3 and I have two possibilities:
  1. Case 1 - Tap on + button on the left, to create a new entry in linked library B. (And then store the entry name in a3).
  2. Case 2 - Tap on the right, to open B library and take the entry name of the selected entry in B.

1 - In case 1, library B is opened to create a new entry. What I want is: Automatically fill the field b1 in B with field a0 from it´s parent library A.
2 - In case 2, library B is opened to select an existing entry of B library. What I want is: Automatically (update or create if it doesn't exist) the field b1 in B with field a0 from it´s parent library A.

Thanks in advance for the help.

Bill Crews

unread,
Nov 18, 2016, 10:20:38 AM11/18/16
to mementodatabase
If I understand you correctly, there's a simple answer for you.

Instead of defining b1 to be an Integer, define it as a Calculation. Within the definition of a calculation is a choice of Result type. Select Integer result. Then, there's a space for entry of an expression. While in this section, press the +Field button, and you'll get a drop-down list containing the fields of the current library and of any linked library. The linked ones will be of the form library.field, so you should see A.a0 as one of the choices. Just select that field, and say you're done, and you should have what you're shooting for.

Pedro Mapo

unread,
Nov 18, 2016, 3:08:05 PM11/18/16
to mementodatabase
That´s no posible. Because The parent library is A not B. 
From A I can easily  access the fields in B with a calculated field, like you mention. 
But what I want is access to the fields in A  from B while A is not already saved.
I used an integer in b1, because I think there´s a way to do it with trigers and javascript.

Then I´ll take a screenshots to make me better understand.

Bill Crews

unread,
Nov 18, 2016, 11:50:55 PM11/18/16
to mementodatabase
Normally, a parent might have several or many children, but not likely the reverse; so the child links to the parent. So, maybe this isn't a one-to-many relationship? For the parent to link to its children, maybe it's many-to-many or one-to-one?

So, I'm confused. Let's not worry about which library we call the parent and focus on what you want to do. I'm assuming now that it is a one-to-one relationship. Otherwise, it won't work, because if many a3s link to a b0, each b1 will get clobbered by the a0s of multiple entries.

You can certainly implement a trigger of type "Create a new entry / Before saving the entry", where a0 will have its new value already. The script can follow the link and, if permission is set for it, plug b1 with the value of a0. The tricks would be:

1. Set permission for the trigger to access library B.
2. var b = entry().field("a3"); // b is an array now, though you say there will be only one entry, so b.length is 1, and b[0] is the entry
3. b[0].set("b1", entry().field("a0")); // Set the value

Pedro Mapo

unread,
Nov 21, 2016, 3:41:08 PM11/21/16
to mementodatabase
After having thought a little more about the problem. I think the solution is not possible, at least with the tools that Memento gives us at the moment.
Anyway, I appreciate the interest you showed in helping me.
Even so, after having made some screenshots and having written step by step what I wanted to do ...
I leave this writing and screenshots, hoping to be wrong, or in the hope that in a future Memento does offer the tools to carry out the resolution of the problem raised.
Thank you.


The scripts would be something like this ...

1 - Script that would execute just when the focus of the new entry creation screen of the library A is lost:
Var b = entry ().field ("a0"); // store in the variable b the value of field a0

2 - Script that would execute just when the focus of the new entry creation screen of the library B is recived:
// take the value of variable b and assign it to the value of field b1 in library B

3 - Script that would execute just when the screen that shows all the entries in B receives the focus
// take the value of variable b and assign it to the value of field b1 in library B if it is not still asigned or update it if it's other
Screenshots.zip

Pedro Mapo

unread,
Dec 13, 2016, 4:15:07 PM12/13/16
to mementodatabase
Finally. The solution:
The script:

var a = entry().field("a0");       // variable a stores the value of field a0 from current entry.
var d = entry().field("a1");       // variable d stores the value of field a1 from current entry.
var b = entry().field("a3");       // variable b stores the value of field a3 from current entry.
                                           // b is an array and its elements are B entry objects related with A.
                                           // if you try to see message(b); you will see something like:
                                           // [com.luckydroid.droidbase.triggers.objects.JSEntry@422f5780,
                                           //  com.luckydroid.droidbase.triggers.objects.JSEntry@42540610,
                                           //  com.luckydroid.droidbase.triggers.objects.JSEntry@42564523].
                                           // Firstly I expected something like [0,1,2] wich are the entryNames of related entries.
                                           // but I was wrong. Because b is in fact an array of entry objects from B.
var l = b.length;                    // to know how many entries in B are related with the entry in A.
for ( var j = 0; j < l; j++){        // loop for all the elements on array b
    var i = b[j];
    var x = i.field("b0");           // I store in x the value of entryName b0 from every entry object in b. I´ll use it later. 
    var y = libByName("B");    // I take the entries of B library.
    var t = y.findByKey(x);      // I get the target entry in B, with the help of its entryName.
    t.set("b1",a);                    // I set the value of a0 field (primary key of A) in b1 field from the target entry in B.
    t.set("b2",d);                    // I set the value of a1 field (date) in b2 field from the target entry in B.
}                                         // end of the loop and the script.

// That´s the end of the script.
memento_v4.1.0 Triggers 20161213_2107.db
Reply all
Reply to author
Forward
0 new messages