javascript include() help!

136 views
Skip to first unread message

Doris Reilly

unread,
May 29, 2025, 11:09:19 PM5/29/25
to mementodatabase
Mmm...I need your help!

This is a follow-up to a previous conversation: https://groups.google.com/u/1/g/mementodatabase/c/whY8QmjdIA4

In that convo I was successful in writing a script to link records in a one-to-many relationship.  But what I actually need to do is process three Access tables related as follows:

Items
itemid
attachmentid
itemname

Attxref
itemid
attachmentid

Attachments
attachmentid
filename

One record in ITEMS will have many records in ATTXREF, and one record in ATTACHMENTS will also have many records in ATTXREF.

I have created two libraries in Memento:

Main
Itemid
Itemname
Attachments  (link-to-entry field, many-to-many to mmAttachments)

Home Inventory Attachments
Itemid
Attachmentid

Home Inventory Attachments Xref
Attachmentid
Filename

The mmAttxref library is only needed temporarily to run the script and populate the link-to-field column Attachments in mmItems.

From previous examples you provided, I got as far as creating a filtered array of records from Home Inventory Attachments Xref for an Itemid in Main.  But my knowledge and experience with javascript is limited, and I can't seem to figure out how to create a filtered array of Home Inventory Attachments records and populate the Attachments column in Main.

Below is what I have so far and I know works.  I looked at push() and other methods, but could not figure it out.  I am hitting my head against the wall, so any help you can provide is appreciated.

Thanks for your help...

Doris

*******************

let main = selectedEntries();
let att = libByName('Home Inventory Attachments').entries();
let xref = libByName('Home Inventory Attachments Xref').entries();

let finditem = '';
let findatt = '';
let mainfilter = [];
let attfilter = [];
let xreffilter = [];

let i = main.length;  // number of selected entries in Main library
//message('Number of Items: ' + i);

while (i--) {
  finditem = main[i].field('ItemID');  // get ItemID for record in Main

  if (finditem > 0 && !main[i].field('Linked')) {   // check if the record has been linked
    message('Processing ItemID ' + finditem);
    xreffilter = xref.filter(a => a.field('ItemID') === finditem);  // get the related attachment xref records

    let j = xreffilter.length;
    message('Number of attachments in xref: ' + j);
    while (j--) {
      findatt = xreffilter[j].field('AttachmentID');  // get AttachmentID for first selected record in xref
      message('Processing AttachmentID: ' + findatt);

      if (findatt > 0) {
        attfilter = att.filter(a => a.field('AttachmentID') === findatt);  // get the related attachments record
        message('Numbrt of attachment records found: ' + attfilter.length);

// THIS IS WHERE I GET STUCK :(

      }
    }
  }
}

Mmm

unread,
May 30, 2025, 7:05:41 AM5/30/25
to mementodatabase
Проверьте:

>>>  let xref = libByName('Home Inventory Attachments Xref').entries();
и
>>>  xreffilter = xref.filter(a => a.field('ItemID') === finditem);  // get the related attachment xref records

В библиотеке 'Home Inventory Attachments Xref' нет поля 'ItemID'.

пятница, 30 мая 2025 г. в 06:09:19 UTC+3, doris....@gmail.com:

Doris Reilly

unread,
May 30, 2025, 9:10:17 AM5/30/25
to Mmm, mementodatabase

My mistake. I mislabeled the libraries. Here are the three libraries I am trying to process:

Main
Itemid
Itemname
Attachments  (link-to-entry field, many-to-many to mmAttachments)

Home Inventory Attachments Xref
Itemid
Attachmentid

Home Inventory Attachments
Attachmentid
Filename

Doris


--
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 visit https://groups.google.com/d/msgid/mementodatabase/faf7ac24-a762-4518-b37c-1327b57ff89an%40googlegroups.com.

Mmm

unread,
May 30, 2025, 10:25:03 AM5/30/25
to mementodatabase
let att = libByName('Home Inventory Attachments').entries();
let xref = libByName('Home Inventory Attachments Xref').entries();

let main = selectedEntries().filter(a => a.field('ItemID').length > 0 && a.field('Attachments').length == 0);// check if the record has been linked

for (let es of main) {

  let mainfilter = [];
  let finditem = es.field('ItemID');  // get ItemID for record in Main


  message('Processing ItemID ' + finditem);

  let xreffilter = xref.filter(a => a.field('ItemID') === finditem && a.field('AttachmentID').length > 0);  // get the related attachment xref records

  message('Number of attachments in xref: ' + xreffilter.length);

  for (let ex of xreffilter) {
    let findatt = ex.field('AttachmentID');  // get AttachmentID for first selected record in xref

    message('Processing AttachmentID: ' + findatt);

    let attfilter = att.filter(a => a.field('AttachmentID') === findatt);  // get the related attachments record

    message('Numbrt of attachment records found: ' + attfilter.length);

    for (let ea of attfilter) {
      mainfilter.push(ea);
    }
  }
 
  if (mainfilter.length > 0) {
    es.set('Attachments', mainfilter);
  }
}

message(' Done! ');

Проверьте названия полей в скрипте и Ваших библиотеках.

пятница, 30 мая 2025 г. в 16:10:17 UTC+3, doris....@gmail.com:
Reply all
Reply to author
Forward
0 new messages