Server Event. Code execution

Hello, I wrote the question in the following post...

Parents
  • Hello, sorry, The forum is terrible for me, I can see it, but I can't create new posts from my computer, I have to do it from my mobile. But from my computer, i can answer other posts. A disaster.

    what I was going to ask.

    I have a server side event. in the onBeforeLock event.

    Basically, I want to create an RM (itemtype ProdRef), but first I want to check that there isn't already one linked to another field of the itemtype.

    The funny thing is that it executes all the code and then gives me the error message.

    var innovator = this.getInnovator();
    
    var ID = this.getID();
    
    // primero comprobamos que no exista ya un RM para este MAT. SUM.
    var codigoAML = "<AML><Item type='MT ProdRef' action='get' select='mp_prodref_mat'><mp_prodref_mat>"+ID+"</mp_prodref_mat></Item></AML>";
    var SentenciaRM =  innovator.applyAML(codigoAML);
    
    if (SentenciaRM.getItemCount() > 0 ){
        return innovator.newError("ERROR: Ya existe un RM para este Material Suministrad0000o. "+ SentenciaRM);
    } else {
    
    // si no existe, continuamos con el proceso.
    
    // cogemos los datos de cantidad y el PN. No puedo usar un this, es el lado del servidor, supongo
    var Datos_MAT_SUM = innovator.applyAML("<AML><Item type='MP_MAT_SUM' action='get'><id>"+ID+"</id></Item></AML>"); 
    var Cantidad = Datos_MAT_SUM.getProperty("mp_matsum_quant","NO CANTIDAD");
    var Origen = Datos_MAT_SUM.getProperty("mp_matsum_orig","NO ORIGEN");
    
    // cogemos el indice del PART
    var DatosPART = innovator.applyAML("<AML><Item type='Part' action='get' select ='mt_par_index'><id>"+Origen+"</id></Item></AML>"); 
    var Indice = DatosPART.getProperty("mt_par_index","0");
    
    //Creamos el RM y le asociamos el MAT. SUM.
    codigoAML = "<AML><Item type='MT ProdRef' action='add'><mp_prodref_mat>"+ID+"</mp_prodref_mat><mt_proref_pn>"+Origen+"</mt_proref_pn><mt_proref_index>"+Indice+"</mt_proref_index><mt_proref_qinit>"+Cantidad+"</mt_proref_qinit><mt_proref_quant>0</mt_proref_quant><mt_proref_qused>0</mt_proref_qused><mt_proref_qfree>"+Cantidad+"</mt_proref_qfree></Item></AML>";
    var SentenciaAML =  innovator.applyAML(codigoAML);
    
    // le asignamos al MAT. SUM. el RM creado
    var id_RM = SentenciaAML.getProperty("id","NO ID");
    
    //Añadimos el codigo del RM y su estado en MAT SUM
    codigoAML = "<AML><Item type='MP_MAT_SUM' action='edit' id='"+ID+"'><mp_matsum_rm>"+id_RM+"</mp_matsum_rm></Item></AML>";
    SentenciaAML =  innovator.applyAML(codigoAML);
    
    return this;
    
    }
    

    In the line 9 i have a getItemCount, but before it shows me the message, it has already executed all the code, it has created the RM and that's how it shows me the message. The bad thing is that since I created the RM before, it always tells me that there is already an RM for that code.

    I have tried to put all the code after the if in an else. And what it does is that it executes the else and then what is in the if. I don't make sense of this

    Any idea??. Too many thanks!!!

  • What ItemType is your onBeforeLock event linked to?

  • In this case your onBeforeLock method calls twice. The second is a result of line 32 of your code. The "edit" action locks, updates and unlocks item. So this lock lead to call your method again. Add "serverEvents='0'" to "codigoAML".

  • Ohh, i was just thinking about it, the action="edit"!!!!.

    I have put, "serverEvents='0'" but it does not work, and it is the same as in other applyAML that there are in the program.

    codigoAML = "<AML><Item type='MP_MAT_SUM' action='edit' id='"+ID+"' serverEvents='0'><mp_matsum_rm>"+id_RM+"</mp_matsum_rm></Item></AML>";
    SentenciaAML =  innovator.applyAML(codigoAML);

  • Oh, I forgot that serverEvents doesn't affect on lock events. So the only way to avoid this re-call is to use the fake property.

    Add something like this to the beginning of your method

    if (this.getProperty("skipOnBeforeLock") == "1") return this;

    and <skipOnBeforeLock>1</skipOnBeforeLock> to codigoAML

  • interesting "fake properties". I am reading it.

    Could the problem be that I'm trying to update and not add, so the itemtype doesn't have that property?.

  • I read your code more carefully and I think that this error occur not in your code, but inside Aras. And it happens because you are trying to edit the same item inside the onBeforeLock event handler of which you are already. Before our changes, the error was generated by your code before attempting to change.

    I think the editing item inside it's own onBeforeLock event is a bad idea.

    Are you really need this code running before lock? Maybe other events would be a better choice? Inside the onBeforeUpdate and onBeforeAdd events, for example,  you don't need an additional "edit" to change the properties of updating item. Just change the properties of "this".

  • Yes I think you are right. I'm going to talk to the person who asked me to run in the onBeforeLock.

    Also, if you notice, I can't use "this", I have to do a "get", lines 6 to 9, even though I have the data on the screen. This happens to me sometimes. Because either I receive a null value, or it tells me "Not a single Item"

    In the end, for now, what I did was create a property in the itemType "skipOnBeforeLock" and I used the code that you proposed. It works perfectly. it's like a fake property, but with the created property.

    2 days fighting with ARAS, for 40 lines of code, OMG!!!

    Thank you very much for your help!!! Thank you!!

    Innovator innovator = this.getInnovator();
    
    var ID = this.getID();
    
    // cogemos los datos de cantidad y el PN. No puedo usar un this, es el lado del servidor, Es por el tipo de evento desde donde es llamado
    var Datos_MAT_SUM = innovator.applyAML("<AML><Item type='MP_MAT_SUM' action='get'><id>"+ID+"</id></Item></AML>"); 
    var Cantidad = Datos_MAT_SUM.getProperty("mp_matsum_quant","NO CANTIDAD");
    var Origen = Datos_MAT_SUM.getProperty("mp_matsum_orig","NO ORIGEN");
    var skipOnBeforeLock = Datos_MAT_SUM.getProperty("skiponbeforelock",""); 
    
    if (skipOnBeforeLock == "1") return this;
    
    // primero comprobamos que no exista ya un RM para este MAT. SUM.
    var codigoAML = "<AML><Item type='MT ProdRef' action='get' select='mp_prodref_mat'><mp_prodref_mat>"+ID+"</mp_prodref_mat></Item></AML>";
    var SentenciaRM =  innovator.applyAML(codigoAML);
    
    if (SentenciaRM.getItemCount() > 0 ) return this;
    
    // si no existe, continuamos con el proceso.
    
    // cogemos el indice del PART
    var DatosPART = innovator.applyAML("<AML><Item type='Part' action='get' select ='mt_par_index'><id>"+Origen+"</id></Item></AML>"); 
    var Indice = DatosPART.getProperty("mt_par_index","0");
    
    //Creamos el RM y le asociamos el MAT. SUM.
    codigoAML = "<AML><Item type='MT ProdRef' action='add'><mp_prodref_mat>"+ID+"</mp_prodref_mat><mt_proref_pn>"+Origen+"</mt_proref_pn><mt_proref_index>"+Indice+"</mt_proref_index><mt_proref_qinit>"+Cantidad+"</mt_proref_qinit><mt_proref_quant>0</mt_proref_quant><mt_proref_qused>0</mt_proref_qused><mt_proref_qfree>"+Cantidad+"</mt_proref_qfree></Item></AML>";
    var SentenciaAML =  innovator.applyAML(codigoAML);
    
    // le asignamos al MAT. SUM. el RM creado
    var id_RM = SentenciaAML.getProperty("id","NO ID");
    
    //Añadimos el codigo del RM y su estado en MAT SUM
    codigoAML = "<AML><Item type='MP_MAT_SUM' action='edit' id='"+ID+"'><mp_matsum_rm>"+id_RM+"</mp_matsum_rm><skiponbeforelock>1</skiponbeforelock></Item></AML>";
    SentenciaAML =  innovator.applyAML(codigoAML);
    
    //return innovator.newError("ERROR: Ya existe un RM para este Material Suministrado. "+ SentenciaRM);
    return this;
    
    
    
    
    

  • I think all your problems are due to an unfortunate choice of the event type. You need an additional "get" and "edit". Inside the onBeforeUpdate and onBeforeAdd "this" always has all changed or added properties to post them to the database and you are free to modify or add any of them without additional query.

    In the end, for now, what I did was create a property in the itemType "skipOnBeforeLock" and I used the code that you proposed. It works perfectly. it's like a fake property, but with the created property.

    This is a very bad idea. Now your onBeforeLock will run only once for each item. All subsequent claims of this item will read "1" as "skiponbeforelock" from database and immediately returns. Is this exactly what you wanted?

  • Hello, yes, yes, i know. Whem itemType "RM" is deleted, it return to '0' value. its controled.

    Thanks!!

Reply Children
No Data