Link to entry - calculation

199 views
Skip to first unread message

Bartłomiej Boruta

unread,
Sep 26, 2019, 5:40:54 AM9/26/19
to mementodatabase
Hi
Two databases connected: Link to entry (Many to many).
How do get the number of links connected?
Example:
base1 - software
base2 - users
How many users are connected to the program.
Tnx

Dan Cox

unread,
Sep 27, 2019, 8:56:56 AM9/27/19
to mementodatabase
Create a JavaScript field in you're "Software" base and call it something like "User Count". In the script for the "User Count" field, paste the script below and then modify the link field name and amount field name below to work for your databases / libraries. If you don't have an amount field in your "Users" library, then you can either create an amount field that always defaults to "1", or you can modify the summation loop in the JavaScript below to just count the number of times that it loops, instead of summing an amount field each time through the loop. Actually, now that I think about it, it looks like the variable linkedEntries.length will just automatically contain the user count that you are looking for.

var amountField = "Chair Occupancy"; // CHANGE Amount to your amount field name
var linkedEntries = field("Guests"); // CHANGE Them to your link field name
var numLE = linkedEntries.length; // The number of linked entries  
// Loop through linked entries, creating the sum
for (var lex = 0, result = 0; lex < numLE; lex++) result += linkedEntries[lex].field(amountField);   result; // The result is the SUM

Dan Cox

unread,
Sep 27, 2019, 9:02:08 AM9/27/19
to mementodatabase
You will probably also want to enable the option in your JavaScript field to "execute script real time" to make sure that your user count is always up-to-date

Bartłomiej Boruta

unread,
Sep 30, 2019, 1:46:55 AM9/30/19
to mementodatabase
Thanks a lot!
It works perfectly :)

Dan Cox

unread,
Sep 30, 2019, 10:05:46 AM9/30/19
to mementodatabase
That is great news! Thanks for letting me know.

DTs EMT

unread,
Feb 26, 2020, 8:28:28 AM2/26/20
to mementodatabase
This is great, but I wonder if there's a way to do it the other way around?

I have two databases:

database1 - EMS calls; each call shows my partner's name; currently has about 3,000 records, linked (many to many) to
database2 - Partner names, a few dozen

When I pull up Partner database, at the bottom I see the link to EMS calls, and a list showing each call I've worked with that partner. I'd like to show a separate field, Count, with the number of calls in that list.

I've tried a simple javascript field in Partner:  field("EMS calls").length;

It always returns the number 25, for each partner.  Very confusing.

I've tried to brute-force it by accessing libByName("EMS calls") and counting every call where that partner name appears, but libByName seems to be deprecated.

Anyone have any suggestions?

Thanks!
Reply all
Reply to author
Forward
0 new messages