automatic link to entry field

268 views
Skip to first unread message

Jean Freitas

unread,
Feb 2, 2018, 10:08:51 AM2/2/18
to mementodatabase
Hi, 

I'm new memento user (bought pro yesterday) and I'm starting my first project. I know that there are a lot of topics about this question, I tried a lot of them and didn't find a solution yet.

I'm creating a project for my personal budgets, basicly my settings are:

Expenses Library
Incomes Library
Bank Accounts Library (With a link to entry for each of other libraries: Expenses, Incomes)

In the Banks Accounts library, I have a calculate field which picks the total amount of expenses or incomes and sums, subtraction from the balance field.

All this is working fine, but I'm creating a expense for instance,  I can't see that bank accounts field to set the relation, what I need to do is save the expense first, and them select the Bank Accounts.

With this configuration is possible to let's say, create a drop-down field in the expenses library for the accounts and based on this field select the relation between that expense and the account from Bank Accounts library?

If this is not the best way to doing this, please give me a suggestion, but for me is really working well besides the fact of this link field.

Note: I don't know much about java so any help would be helpfull.

Thank you

Bill Crews

unread,
Feb 2, 2018, 10:39:10 AM2/2/18
to Jean Freitas, mementodatabase
Once you've created Accounts, when you create Expenses (or Incomes), then when you add a Link to Entry field to it, you will see the Accounts library in the list and can set that field to point to Accounts. Then set the field to one-to-many, if that's appropriate.

Later, while adding expense entries, you can tap the link field and choose the specific account the expense is for.

Jean Freitas

unread,
Feb 2, 2018, 10:54:19 AM2/2/18
to mementodatabase
I did the opposite, created the link to entry field in the Accounts library and selected the Expense (or Incomes) library.
The reason why I did that was to be able to calculate the fields from that libraries, when I tried to do as you suggested, the fields didn't show in the Accounts library in the calculate formula.

Jean Freitas

unread,
Feb 2, 2018, 11:08:35 AM2/2/18
to mementodatabase
To clarify my question:

I followed your solution and I'm able to see the link to entry field when I'm creating an entry. Now how I'd be able to calculate the amount field from expense library in the Accounts library?

before, when my link was created in the accounts library pointing to expenses, I used a calculated field like this:

#{starting.balance} - #{expe...@sum.amount} (again, this in the accounts library)

If I try to do the same but with the link to entry created in the expenses library and poiting to the account one, this doesn't work.

Bill Crews

unread,
Feb 2, 2018, 11:17:00 AM2/2/18
to Jean Freitas, mementodatabase
OK, you can do it that way, but the calculations will only be in Accounts. If you do it that way, it must be a many-to-many link, which shows the list of existing expenses in the account entry. So, to add an expense, you have to first go to the account it is for, edit the entry, and press the plus symbol on the link to expenses to add the expense. You do your adds through Accounts, not directly.

I'm still playing with these many-to-many links, though I'm used to the other, relational way. It has some interesting features, but so far, I still lean toward one-to-many from the child library.

Check how aggregation works when you do it this way. Also, the list of expenses you get is inside the Accounts entry view card and is not a real Memento entries list, so there's no slicing & dicing (sorting, grouping, filtering, charting, etc). In my experience so far, if you want to do those things, it'll work better with one-to-many from child to parent.

Let us know what you find.

Bill Crews

unread,
Feb 2, 2018, 11:27:20 AM2/2/18
to Jean Freitas, mementodatabase
Typically, instead of calculating an account balance as a field, you'd let aggregation do it. Set the library (Main tab) to show aggregations. In the Expenses entries list, group by account. Then, aggregation can show the balance on the group bar.

If you really want balance fields, you can create an entry action, a library action, or (best) a trigger that'll run when the Accounts library opens to calculate the balances. It'll run a little slower, but you'll never have the syndrome of balances out of sync with detail data, which plagues systems that try to keep balances within data.

Again, there are always tradeoffs; I don't think there's a perfectly right or wrong way to do it.

Jean Freitas

unread,
Feb 2, 2018, 12:19:40 PM2/2/18
to mementodatabase
Thank you.

I'll try create a new library using your suggestions and let you know how that went.

Jean Freitas

unread,
Feb 2, 2018, 2:48:39 PM2/2/18
to mementodatabase
Hi Bill,

I have another question. In the new library I'm trying to create a calculation field based on the option of the other field:

If type = expense then sum.amount


How can I achieve this ?

Bill Crews

unread,
Feb 2, 2018, 3:57:27 PM2/2/18
to Jean Freitas, mementodatabase
I forget if you told us how you're doing things, but assuming you are calculating to a value of a field in a library entry, you could use a JavaScript field. Something like this...

if (field("Type") == "Expense")
   -field("Amount");
else
   field("Amount");

If you are in a Updating an entry Before saving trigger script in Accounts, you could have something like...

var e = entry();
if (e.field("Type") == "Expense")
   e.set("Balance", e.field("Balance") - e.field("Amount"));
else
   e.set("Balance", e.field("Balance") + e.field("Amount"));

So, when I just wrote that, a question entered my mind. If you're linked down (many-to-many parent to child), when you add an entry to Expenses, it appears as Creating a new entry to Expenses, but as Updating an existing entry to Accounts -- an interesting thing to keep in mind if you have triggers in both places. What I am most accustomed to doing is linking bottom up, so Accounts has no knowledge of my adding an entry to Expenses. Only the Expenses trigger fires.

Also, per our earlier conversation, I confess that I have a couple of often-used libraries right now that link both ways. The main link is from the child back to the parent, but sometimes when I'm in the parent, I want to see the children, so I link to the child from the parent, also. I know I could get caught chasing/eating my tail, though, so I always keep in mind that the parent-to-child link is secondary, but so far, so good, with my current usage. I could maybe get in trouble when I start deleting entries, I suppose. We'll see.

Jean Freitas

unread,
Feb 5, 2018, 2:09:12 PM2/5/18
to mementodatabase
Hi Bill,

Just want to share my solution in case other people needs the same thing.

So I stayed with two libraries, Expenses and Bank Accounts. 

When I create a entry in the Expenses account the trigger will automatically set the link based on the account field (expenses library).

Here is the trigger (creating a entry, after save)
//New entry
var newEntry = entry();

//Check account field 
var myField = entry ().field("Account");

//Set link field name 
var linkFieldName = "Expenses";

 //Set library name
var myLibrary = libByName("Bank Accounts");

//Checking if the Expenses library is accessible
if (myLibrary == null) {
       message("Couldn't access " + myLibrary + " Library");
}

//Find the entry in myLibrary that matches with myField
var myLibrarySearch = myLibrary.find(myField);

//Return number of entries
//message(myLibrarySearch.length);

//Set the entry based in the previous search
var myLinkEntry = myLibrarySearch[0];

//Link new entry with myLibrary link entry
myLinkEntry.link("Expenses", newEntry);

message("Expense added to " + myField + " account ")

Hope it helps other people.

Indrawan alghifary

unread,
Dec 22, 2019, 10:37:53 AM12/22/19
to mementodatabase
I have a problem, I can't run javascript, like the log result, can you help me? or you give me your library template? Thank you
Screenshot_2019-12-22-22-30-13-641_com.luckydroid.droidbase.jpg

Bill Crews

unread,
Dec 24, 2019, 9:35:48 AM12/24/19
to Indrawan alghifary, mementodatabase
You have an error, maybe a syntax error, on line #20. I can't tell what it might be, since you didn't show us the script.

--
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/f386d5c4-8c34-4051-aa72-0381ad38e512%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages