Copying value of text field to linked entry

127 views
Skip to first unread message

Radek B.

unread,
Feb 20, 2018, 8:48:46 AM2/20/18
to mementodatabase
Hello!
I am trying to learn this Java Script thing but it really is too much for me when it comes to Scripts. Now I even wonder is it possible to do what I want.

I have one library and among many others there is one field, let's call it "field1". I want to copy a value from "field1" to every new entry which is created and linked to the old entry.

For example:
In case of old_entry "field1" is 5B, I want to create and link to old_entry, two new ones: new_entry1, and new_entry2. I want for both of them to inherit for their "field1" a value 5B.

 I suppose I need to make a script for it with a trigger: Linking an entry/Before saving it. Unfortunately I have no idea what to do next, mostly because of not understanding how to get a value from the old_entry.

I hope I had explained it properly.

Kind regards
Radek


Bill Crews

unread,
Feb 21, 2018, 9:31:22 AM2/21/18
to Radek B., mementodatabase
I'm not sure I understand; let me try...

You are working in this case within a single library. One of the fields is a Link to Entry field pointed back into this same library, allowing an entry to link to one or more other entries in the library, yes? In addition, you want the value of a field in a linked entry to appear in the outer entry, yes?

Well, if I've understood that correctly, then first, it must be a one-to-many relationship, so that there is a unique entry whose field1 value is to be referenced.

Then, to reference it, you'll need no trigger, just a JavaScript field (maybe called linkedField1) referencing the field1 value in that unique linked entry, as follows... (Assume link field is called link1.)

field("link1")[0].field("field1")

Radek B.

unread,
Feb 21, 2018, 11:20:00 AM2/21/18
to mementodatabase
Not exactly. I was thinking about the way things are in my libraries, and I have reworked it a little. This way it should work better for me and will be easier to explain/understand my problem. I also translated the most important parts.

I thought that a picture is worth a thousand words so I attached one.

44, @34, #1, 03.2 - text field "ID". Field "Area" is also a text one. I think just JavaScript field is no-no because of the custom values. The best option would be probably entryDefault?

Is this more clear now?
example.jpg

Bill Crews

unread,
Feb 21, 2018, 11:41:10 AM2/21/18
to Radek B., mementodatabase
Do a trigger if you want. If you do, use entryDefault for a Creating trigger and entry for an Updating trigger. You'll need both.

But I don't get why you think a JavaScript field won't work? Yes, it would be best to add the following to the scriptlet...

if (field("link1") != null)
   field("link1")[0].field("field1")
else
   // Whatever else for other cases

In terms of performance, triggers run at the specified time, often upon saving, while JavaScript fields are evaluated whenever an entry is viewed and perhaps when generating a list, if not using a regular field.

Radek B.

unread,
Feb 21, 2018, 12:35:10 PM2/21/18
to mementodatabase
JavaScript field won't work for me because I won't to copy only a text from the field not the entire field. I want to have a possibility to manually edit this field later, and in other cases just type it in.

I think I have used wrong words. Correct me if I am wrong? If I understand well this code gets a value from the first linked. Yeah? I am looking more for copying a value from the original and first entry to which other are linked to the linked ones.

Bill Crews

unread,
Feb 21, 2018, 1:43:28 PM2/21/18
to Radek B., mementodatabase
OK, got it. Of course, if you change the value in the 03 entry, the values in 03.1 & other dependent fields will not change, unless to provide enough trigger logic to make it so.

So, I'm envisioning a trigger for Event Linking an entry Phase After saving an entry. You might consider what you would want to do if the user unlinks the entry; whatever that might be, you could put it into a trigger upon Event Unlink an entry.

The Linking trigger script would be something like...

var e = entry();
e.set("field1copy", e.field("link1")[0].field("field1"));

This script will run each time the user links to an entry.
Message has been deleted

Radek B.

unread,
Feb 21, 2018, 5:00:06 PM2/21/18
to mementodatabase


W dniu środa, 21 lutego 2018 22:58:33 UTC+1 użytkownik Radek B. napisał:
I think I will give up. Nothing seems to work. I have tried to rework links, orders and many things. In most of the cases Memento keeps reporting that it can't execute script: Cannot call method "field" of undefined (in regard to field("link1"). I will use the example trigger: "Previous value of another field" and live with that for now.
Thank you for your time Bill.

Bill Crews

unread,
Feb 21, 2018, 5:50:27 PM2/21/18
to Radek B., mementodatabase
I'm sorry you're giving up. I don't know anything about orders, but the only thing I can think of that would result in that error message is if you didn't change the field names to match your field names. This is the first time I've heard of your having this message.

You have a Link to Entry field. Please replace "link1" with its name. Replace "field1" with the name of the field whose value you want to copy. And replace "field1copy" with the name of the field you want the copy to go into. The resulting statements should still have double-quotes, but not quotes within quotes.

There are various situations that can result in an expression being undefined. Getting used to how those things happen is part of becoming a JavaScript programmer. It's not easy to explain in a post. Persevering through these times is a passage to the next level.
Reply all
Reply to author
Forward
0 new messages