Copy record from a library to another

285 views
Skip to first unread message

Diego Marino

unread,
Jul 31, 2018, 1:43:58 PM7/31/18
to mementodatabase
Hi,
i'd like to copy a record (in specific a contact) form my "contacts" library to another library, with the same structure except that the new structure has not every fields and pages than the original

how may i do that?

Bill Crews

unread,
Aug 1, 2018, 3:52:50 PM8/1/18
to Diego Marino, mementodatabase
You can create an entry action to do it. The fields of the entry can be referenced by entry().field("Field Name"). To create a new entry in another library, you can first grant permission to address that library by tapping the Shield icon in the script editor and selecting the other library, and then...

var there = libByName("Library Name");

Then do a var newEntry = new Object() to make a new Object, then set its properties with newEntry["Field Name"] = value, so you'll end up with something like several...

newEntry.["Field Name There"] = entry().field("Field Name Here"):

After doing all those, then...

there.create(newEntry);

...and you're done.

--
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.
For more options, visit https://groups.google.com/d/optout.

Diego Marino

unread,
Aug 3, 2018, 8:28:21 AM8/3/18
to mementodatabase
thx bill, it seems not too difficult

Diego Marino

unread,
Aug 31, 2018, 6:04:32 AM8/31/18
to mementodatabase
hi bill,
i tried to make that script

var there = libByName("Schede");


var newEntry = new Object()

newEntry.["ID"] = entry().field("ID")
there.create(newEntry)


"schede" is the new library, and i tried to copy just the id to try, but it says "script error, canot call method "field" of null"
Surely i made a stupid error, can you help me?

Diego Marino

unread,
Aug 31, 2018, 11:02:44 AM8/31/18
to mementodatabase
Found the problem!


It is new.Entry["ID"], and not new.Entry.["ID"]

Thx bill

Diego Marino

unread,
Sep 4, 2018, 9:57:05 AM9/4/18
to mementodatabase
hi, it works but i have a small problem on date field
this is cause the copy of a date field is different if i remember, right?

Diego Marino

unread,
Sep 4, 2018, 10:35:49 AM9/4/18
to mementodatabase
the problem is with "link to other library" too, and the field has attributes too.
is there any way to copy date and link to other library field with attributes?

Bill Crews

unread,
Sep 7, 2018, 2:36:16 AM9/7/18
to Diego Marino, mementodatabase
For link-to-entry fields, read this http://wiki.mementodatabase.com/index.php/Tips:Using_JavaScript_in_Memento#Link_to_Entry_fields

In that same page, it talks about date fields, too, but not nearly enough. The main thing on dates is to use getTime() to get milliseconds when you get a value for a comparison, for a calculation, or pretty much anything else. Like this, for a JavaScript field...

if (field("Date").getTime() > field("Deadline").getTime())
   "Overdue";
else
   "On schedule";

Diego Marino

unread,
Sep 10, 2018, 7:06:12 AM9/10/18
to mementodatabase
thx very much, vasya too wrote me
the date field is easy to copy, the only problem is that if the source field is blank ti give me an error, i image that i have to make a condition first, like
if entry.("data di nascita") !=0

can you telle me the correct form?

Bill Crews

unread,
Sep 10, 2018, 7:35:08 AM9/10/18
to Diego Marino, mementodatabase
Well, it's a Date field, so that might not work. I'm pretty sure checking for != null will work for an empty field of any type.

Diego Marino

unread,
Sep 12, 2018, 8:23:20 AM9/12/18
to mementodatabase
Thx very much vasya mailed me

var ultimoTime = entry().field("Ultimo cambio azienda")
if (ultimoTime != null) newEntry["Ultimo cambio azienda"] = ultimoTime .getTime();

this worked!

Bill Crews

unread,
Sep 12, 2018, 9:21:01 AM9/12/18
to Diego Marino, mementodatabase
Interesting. It shouldn't have. There's a missing semicolon after the var statement. I've heard that missing semicolons occasionally work anyway in JavaScript, but I never know when that will be, so I always recommend coding it in.

And the space after ultimoTime and before .getTime(); in the last statement should cause that statement to fail. It should be ultimoTime.getTime();

Diego Marino

unread,
Sep 25, 2018, 9:08:36 AM9/25/18
to mementodatabase
Hi bill,

everything is fine, i'm just waiting for vasya that is helping me with copy of "link to another library field"


i have a question: in this script i copy every field to another library, and i set an argument. ho can i copy the value of the argument to the new library?

var there = libByName("Schede");
var newEntry = new Object()

newEntry["Contratto del credito"] = entry().field("Contratto del credito")
newEntry["Contratto di agenzia"] = entry().field("Contratto di agenzia")
newEntry["Trattativa"] = arg(Trattativa)------------THIS ONE!!!
there.create(newEntry)

this is a small part of the script, i'm interested in the "arg" string (the ONE HIGHLIGHTED)

consider that in the mother library the argument is a "list of values", and in the son library the value chosen will go to a textbox
can you heelp me?

Bill Crews

unread,
Sep 25, 2018, 9:23:15 AM9/25/18
to Diego Marino, mementodatabase
var there = libByName("Schede");
var newEntry = new Object()
newEntry["Contratto del credito"] = entry().field("Contratto del credito")
newEntry["Contratto di agenzia"] = entry().field("Contratto di agenzia")
newEntry["Trattativa"] = arg(Trattativa)------------THIS ONE!!!
there.create(newEntry)

this is a small part of the script, i'm interested in the "arg" string (the ONE HIGHLIGHTED)

consider that in the mother library the argument is a "list of values", and in the son library the value chosen will go to a textbox
can you heelp me?

The arg() global function is specifically for use in action scripts to refer to an argument entered upon invocation of the script by pressing the Play button. If that's not what you're doing, it's the wrong thing to be using.

If, by "list of values", you mean the items of a choice field, like a Radio Buttons or Checkboxes field, then you'll need to use field() to fetch the value, as you do in the previous line of the script. If you mean an element of an array of values, you'll need to use array syntax, like myArray[myIndex]. I don't know what else you might be referring to.

Diego Marino

unread,
Sep 25, 2018, 9:37:39 AM9/25/18
to mementodatabase
Yes, i have a button that i click and it copies fields to the library i'm using into another library creating a new record...

so, when i click the button the script calls the argument that is a list of values from which i choose just one, then in the other library i want to copy that value in a textbox

var there = libByName("Schede");
var newEntry = new Object()
newEntry["Contratto del credito"] = entry().field("Contratto del credito")
newEntry["Contratto di agenzia"] = entry().field("Contratto di agenzia")

newEntry["Trattativa"] = arg(Trattativa).field()
there.create(newEntry)

how is the right way?

Diego Marino

unread,
Sep 25, 2018, 10:09:56 AM9/25/18
to mementodatabase
and just to be safe, this is the correct form?

var ultimoTime = entry().field("Data di nascita")
if (ultimoTime != null) newEntry["Data di nascita"] = ultimoTime.getTime(); 

before it worked, now it gives me an error...

Bill Crews

unread,
Sep 25, 2018, 12:01:56 PM9/25/18
to Diego Marino, mementodatabase
Well, if I recall correctly, I told you once before that the var statement needs a semicolon on the end, and it still does, though this may of may not result in an error.

Now, Date fields are weird, and it's easy to get it wrong...

1. I'm not sure that the right side of the assignment in the first line returns a time value as it's written vs a string, though I am pretty sure that it returns a null, if the field isn't filled in.
2. If you try to be sure by instead saying entry().field("Date di nascita").gettime();
then, I'm not sure that ultimoTime will receive a null, if it's not filled in.

So, to be sure, I'd say...

var e = entry();
if (e.field("Data di nascita") != null)
   newEntry["Data di nascita"] = e.field("Data di nascita").getTime();

If that gets an error, you'll have to tell me what the error is (and which line (number) is in error), if you want me to explain why there is an error.

Diego Marino

unread,
Sep 26, 2018, 5:34:38 AM9/26/18
to mementodatabase
Thanks bill, it says

script error: type error: can't set string '-60097121148' to field Data di nascita (creazione scheda js#52)

it seems that it consider the date a string.....

i tried your suggested script but it's the same...

Bill Crews

unread,
Sep 26, 2018, 8:01:08 AM9/26/18
to Diego Marino, mementodatabase
So, this seems to come from your original code and not mine. That is, that your line 52 was the "var ultimoTime =" line and that, as I posited in my #1, it returns a string. You say it's the same with my script lines, but I don't see how that would be true. Some other error, maybe, but not that one. Please try mine again. I believe there is an error, but it must be a different error.

Diego Marino

unread,
Sep 26, 2018, 9:22:52 AM9/26/18
to mementodatabase
Thanks for the patience, i'm not a coder. I'm learning something right now..

that's the script
var there = libByName("Schede");
var newEntry = new Object();
newEntry["ID"] = entry().field("ID")
newEntry["Nome"] = entry().field("Nome")
newEntry["Cognome"] = entry().field("Cognome")
newEntry["Cellulare"] = entry().field("Cellulare")
newEntry["Telefono fisso"] = entry().field("Telefono fisso")
newEntry["Email personale"] = entry().field("Email personale")
newEntry["Email ufficio"] = entry().field("Email ufficio")
newEntry["Comune"] = entry().field("Comune")
newEntry["Provincia"] = entry().field("Provincia")
newEntry["Regione"] = entry().field("Regione")
newEntry["Area"] = entry().field("Area")
var e = entry();
if (e.field("Data di nascita") != null)
   newEntry["Data di nascita"] = e.field("Data di nascita").getTime();
newEntry["Posizione"] = entry().field("Posizione")
newEntry["Azienda"] = entry().field("Azienda")
newEntry["Settore"] = entry().field("Settore")
newEntry["Ptf banca"] = entry().field("Ptf banca")
newEntry["Ptf trasferibile"] = entry().field("Ptf trasferibile")
newEntry["Note portafoglio"] = entry().field("Note portafoglio")
newEntry["Ptf gestito"] = entry().field("Ptf gestito")
newEntry["Ptf assicurativo"] = entry().field("Ptf assicurativo")
newEntry["Ptf amministrato"] = entry().field("Ptf amministrato")
newEntry["Numero clienti"] = entry().field("Numero clienti")
newEntry["Redditività ptf"] = entry().field("Redditività ptf")
newEntry["Taglio medio cliente (€)"] = entry().field("Taglio medio cliente (€)")
var e = entry();
if (e.field("Scadenza patto") != null)
   newEntry["Scadenza patto"] = e.field("Scadenza patto").getTime();
newEntry["Penale economica"] = entry().field("Penale economica")
newEntry["Patto di non concorrenza"] = entry().field("Patto di non concorrenza")
var e = entry();
if (e.field("Ultimo cambio azienda") != null)
   newEntry["Ultimo cambio azienda"] = e.field("Ultimo cambio azienda").getTime();
newEntry["Tipo di contratto"] = entry().field("Tipo di contratto")
newEntry["Livello"] = entry().field("Livello")
newEntry["RAL/Fatturato"] = entry().field("RAL/Fatturato")
newEntry["Premi"] = entry().field("Premi")
newEntry["Incremento RAL per patto"] = entry().field("Incremento RAL per patto")
newEntry["Iscrizione"] = entry().field("Iscrizione")
newEntry["Iscrizione albo"] = entry().field("Iscrizione albo")
newEntry["Ptf banca gruppo"] = entry().field("Ptf banca gruppo")
newEntry["Ptf trasferibile gruppo"] = entry().field("Ptf trasferibile gruppo")
newEntry["Persone gestite"] = entry().field("Persone gestite")
newEntry["Zone"] = entry().field("Zone")
newEntry["Profilo"] = entry().field("Profilo")
newEntry["Richieste"] = entry().field("Richieste")
newEntry["Motivazione al cambiamento"] = entry().field("Motivazione al cambiamento")
newEntry["Contratto del credito"] = entry().field("Contratto del credito")
newEntry["Contratto di agenzia"] = entry().field("Contratto di agenzia")
there.create(newEntry)


it give me the same error i wrote below....

sacroimper

unread,
Sep 26, 2018, 1:46:51 PM9/26/18
to mementodatabase

Can you tell me if the date is before 01/01/1970?

I think it can be the same error I posted in this forum:

Diego Marino

unread,
Sep 26, 2018, 3:14:13 PM9/26/18
to mementodatabase
you're right. I was writing that only 1 record gave me problem, and in fact it has the date of 5/2/1969...

Diego Marino

unread,
Sep 27, 2018, 6:00:53 AM9/27/18
to mementodatabase
I need to copy the date, but i do not need the new field is a "date field", it can be a text field
i tried to copy but it is a string with numbers.....is it possible to convert to a date?


so i'll have:

mother library--date field
son library---text field (but with a date not a number)....

Bill Crews

unread,
Sep 27, 2018, 7:27:33 AM9/27/18
to Diego Marino, mementodatabase
The moment library (http://momentjs.com/) works very nicely; try it. You'll have to tap on "Add JavaScript libraries..." in your JavaScript field to check the box. Most likely, moment().format() will do what you want, but the whole library can be very useful.

Diego Marino

unread,
Oct 1, 2018, 5:37:11 AM10/1/18
to mementodatabase
found it thanks

of course i have problem with syntaxis..

newEntry["Data nascita"] = entry().field("Data di nascita").moment().format('MMMM Do YYYY, h:mm:ss a')

where is the error? and then, i do not want hours second and minutes....

Bill Crews

unread,
Oct 1, 2018, 10:21:49 AM10/1/18
to Diego Marino, mementodatabase
1. You're still not putting a semicolon at the end of your statement.
2. The format you're using in your format() call includes hours, minutes, and seconds, though you say you don't want them.

So, try...

newEntry["Data nascita"] = entry().field("Data di nascita").moment().format("MMMM Do, YYYY");

If you'd rather have the day be 1 or 2 instead of 1st or 2nd, replace Do with D.

If you'd prefer the month to be Oct instead of October, replace MMMM with MMM.

If you'd prefer a different sequence, just adjust the format() string to your taste.

Diego Marino

unread,
Oct 1, 2018, 11:09:20 AM10/1/18
to mementodatabase
thx you're right about the semicolon

i made this

newEntry["Data nascita"] = entry().field("Data di nascita").moment().format("MMMM D, YYYY");

but it says that "missing ] in index expression"

then i tried to make this one

newEntry[["Data nascita"] = entry().field("Data di nascita").moment().format("MMMM Do, YYYY");
with two "[" before "data nascita" and it says "missing ] after element list

is it an array? i think not

Bill Crews

unread,
Oct 1, 2018, 11:18:33 AM10/1/18
to Diego Marino, mementodatabase
No, the syntax you're using is a reference to a property of an object. This error should not be happening.
  1. Make sure the error message refers to the line you think it is. You can tell by the line number in "js#1" or whatever.
  2. Make sure you don't have any mismatches in brackets, parentheses, or quotes, especially in the line or two just preceding this statement. Make sure single-quotes match with single-quotes and double-quotes match with double-quotes.
  3. Make sure you've properly declared newEntry as an object, with new Object or {} or however else.

Diego Marino

unread,
Oct 2, 2018, 9:49:43 AM10/2/18
to mementodatabase
ok, now i wrote:

newEntry["Data nascita"] = entry().field("Data di nascita").moment().format("MMMM D, YYYY");

but when i try it says"cannot call method "moment" of null
when i use the entry action on a record with "data di nascita" not null it do not copy that record...

Diego Marino

unread,
Oct 8, 2018, 8:47:03 AM10/8/18
to mementodatabase
hi bill, vasya gave me this part of the script to copy link to entry field

var newEntry = there.create(newEntry);
var links = entry() .field("Esperienze precedenti");
for ( var ent = 0 ; ent < links. length ; ent ++) {
newEntry.link("Esperienze precedenti" , link[ent]);
}

he told me i have to put it in the "mother" library, but after the element is created.
what does it mean?
where i have to put this part of the script?

Bill Crews

unread,
Oct 8, 2018, 5:49:36 PM10/8/18
to Diego Marino, mementodatabase
It's best to stick with Mementospeak, and there's nothing there called an element, though JavaScript has things called elements, which are the things that make up an array.

So, when you say element created, I don't know. It probably means entry. If that's what you mean, then he probably means that the create(newEntry) line should come before the linking lines, which it does already in that code snippet. However, in your case, there are other field values you want to set, so let's break his first statement into two, so we can put your other lines in between.

So, since you have other fields also to copy, in the from library, try this variation...

var there = libByName("To Library");
var newEntry = {};
// Replace this line with those setting your other field values
newEntry = there.create(newEntry);     
var links =  entry().field("Esperienze precedenti");  
for   (var ent  =  0 ;  ent  <   links. length ;  ent ++)   { 

   newEntry.link("Esperienze precedenti" , link[ent]); 
}

As for the mother library, I think he means the library that you're linking from -- the one with the Link to Entry field in it. The son library is the one you're linking to. Since both the to and from libraries are mothers, I don't know what he means. However, what I say is that the code needs to be in the library you're copying from.
}

Diego Marino

unread,
Oct 11, 2018, 5:40:47 AM10/11/18
to mementodatabase
thx bill. That's my whole script

i have the message "error missing ; before statement"

var there = libByName("Schede");
var newEntry = new Object();
var newEntry = {};
there.create(newEntry);
Var links = entry().field("Coordinati");
for (var ent = 0; ent < links. Length; ent++)
{
NewEntry.link("Coordinati",link[ent]);
};
Reply all
Reply to author
Forward
0 new messages