Help with a trigger script

166 views
Skip to first unread message

tzarinak...@gmail.com

unread,
Mar 4, 2019, 11:38:34 AM3/4/19
to mementodatabase
Hi everybody.

Need some help with a piece of javascript. I know nothing of javascript, but I have set up a script build upon two of the trigger examples. The first part is the one I think is giving the error. The second part is working good.
The trigger event type are "creating an entry" and "updating an entry" and both in the "after saving the entry" phase.
The goal of the triger script is to create a new entry in another library if the conditions are fulfilled.
I used the "New entry in one library triggers new entry in another" example for the second part of the script and for the first part the "Ensuring unique non-Name field value" example of the Trigger Examples of the wiki.

My group layout is:

INCIDENCIAS library:(entry name is unique, entry name "NÚMERO:"

-NÚMERO:(integer autoincrement)(name)
-CLIENTE:(link to entry to CLIENTES library
-FECHA:(date)(status)
-ETIQUETA:(calculation)(status)
-LUGAR:(location)
-PRIORIDAD:(single-choice list)(status)
-ESTADO:(single-choice list)
-FACTURAR(boolean)
-DESCRIPCIÓN:(text)(description)
-FOTOS:(image)
-HORAS:(real number)

CLIENTES library:(entry name is unique, entry name "Número de cliente:", "Nombre:")

-Número de cliente:(integer autoincrement)(name)
-Nombre:(text)(name)
-NIF:(text)(description)
-Dirección:(text)(description)
-Teléfono:(phone number)(description)
-Email:(text)

FACTURAS libray:(entry name is unique, entry name "Número:")

-Número:(integer autoincrement)(name)
-Número de albarán:(integer)(status)
-Fecha:(text)(status)
-Cliente:(text)(description)
-NIF:(text)
-Dirección:(text)
-Concepto:(text)(description)
-Precio:(integer)(status)

I'm trying to set up a kind of "work order with invoicing" and, of course, lern how to use this wanderful app.
The trigger is set up in the "INCIDENCIAS" library with all permissions.

The script is:


var albaran = entry().field("NÚMERO:");
var numeroDeEntradas = libByName("FACTURAS").entries();
var albaranNoExiste = true;

for (var ent = 0; ent < numeroDeEntradas.length; ent++) {
      if (numeroDeEntradas[ent].field("Número de albarán:") == albaran)
albaranNoExiste = false;
}

var e = entry();
if (albaranNoExiste && e.field("FACTURAR") && e.field("ESTADO:") == "RESUELTO") {
   var factura = libByName("FACTURAS");
   var nuevafactura = new Object();
nuevafactura["Cliente:"] = e.field("CLIENTE:").field("Nombre:");
nuevafactura["Número de albarán:"] = e.field("NÚMERO:");
nuevafactura["Fecha:"] = moment(e.field("FECHA:")).format("DD/MM/YYYY");
nuevafactura["NIF:"] = e.field("CLIENTE:").field("NIF:");
nuevafactura["Dirección:"] = e.field("CLIENTE:").field("Dirección:");
nuevafactura["Precio:"] = e.field("HORAS:")*10;

factura.create(nuevafactura);

}

The darken part was working ok, but I added the first part just to add the "albaranNoExiste" condition in the dark part that is traying to avoid the creation of a new entry in "FACTURAS" library if the current "NÚMERO:" value does already exist as "Número de albarán:" in "FACTURAS" library.

After adding that and testing the script with the "play" button it says "succssecfully executed" but when updating an entry in "INCIDENCIAS" library that matches all the conditions to create a new entry in "FACTURAS" library I get this error:

TypeError: Cannot find function field in object [com.luckydroid.droidbase.triggers.objects.JSEentry@28e69e8]. (Fecturar edición.js#16)


I hope the problem is just a simple thing. As I told before I have no idea of javascript, but I think Memento is going to get lot of us into that world.

Any help will be apreciated.

Thanks.

Bill Crews

unread,
Mar 4, 2019, 12:13:51 PM3/4/19
to tzarinak...@gmail.com, mementodatabase
I wish I could tell you. On line #16 is a reference to the FECHA: field of the INCIDENCIAS library, and that should work, since your surrounding statements all work correctly.

With no initial clue, I suspected the message itself is bogus -- it suggests the object represented by e is not an Entry object, which is clearly not true -- and that the problem might be related to using moment() on a Date object, but that should work also, as documented in the moment.js manual: 

You can create a Moment with a pre-existing native Javascript Date object.

var day = new Date(2011, 9, 16);
var dayWrapper = moment(day);
I've used that myself in the past, such as when I tested the wiki example. So, I don't have a clue, I'm afraid.

tzarinak...@gmail.com

unread,
Mar 20, 2019, 4:23:49 PM3/20/19
to mementodatabase
Hi Bill, Thankyou very much for taking the time to read my long post and for the answer. I din't know the last line of the error message with the hash mark pointed to the code line with the error,....yes I'm at that level......so you have taught me a new thing. You are doing a great job in this forum.
Aftrer doing some test things I'm agree with your diagnosis. Is a kind of strange error because the same code in a new library is working ok.
I have rebuilt the all thing from scratch and works ok. I have change the two triggers for one action that fits better with my workflow.


Thank againg for the help.
Reply all
Reply to author
Forward
0 new messages