Problema con hook after save | After save Hook - Problem

Buenos días, cree un Hook para que se actualice un registro de otro módulo de acuerdo a ciertos parámetros que debe cumplir el registro para que esto pase. Explico la situación:
El hook que cree es un “after_save”;

Cuando se crea un registro en el módulo “x”, si este tiene una relación con un registro del módulo “y”, y a su vez tiene marcado un checkbox, debería modificar el campo "cantidad " en el registro dell modulo “y”.

Funciona:

  1. Creo registro de modulo Y.
  2. Creo registro del modulo X.
  3. Modifico registro del modulo X, en el campo de relacion pongo el registro del modulo “Y”, y sumado a esto, activo el checkbox.
    Resultado: se ve OK el campo modificado.

No funciona:

  1. Creo registro modulo Y.
  2. Creo registro modulo X con el checkbox activado y el campo relacionado con el registro del modulo “Y”.
    Resultado: No se ve el campo modificado. Pero si lo edito y guardo denuevo, si.

Me imagino que esto viene por lo siguiente:
Al crear el registro, al mismo tiempo se crea la relación, pero el hook se dispara antes y todavía no está creada por lo que queda sin efecto.

¿Cómo puedo hacer para que esto funcione desde la primera creación del registro?

Muchas gracias desde ya


Good morning,

I created a hook to update a record from another module according to certain parameters that the record must meet for this to happen. Let me explain the situation: The hook I created is an “after_save” hook.

When a record is created in module “x”, if it has a relationship with a record from module “y”, and that record also has a checkbox marked, it should modify the “quantity” field in the record of module “y”.

It works:

  1. I create a record in module Y.
  2. I create a record in module X.
  3. I modify the record in module X, set the related record from module “Y” in the relationship field, and additionally, I check the checkbox.

Result: The modified field is correctly displayed.

It doesn’t work:

  1. I create a record in module Y.
  2. I create a record in module X with the checkbox checked and the related field pointing to the record from module “Y”.

Result: The modified field is not visible. But if I edit it and save it again, it works.

I imagine this issue is caused by the following: When creating the record, the relationship is created simultaneously, but the hook triggers before it’s established, rendering it ineffective.

How can I make this work from the initial record creation?

Thank you very much in advance.


<?php


class UpdateOferta{

function updateData($bean, $event, $arguments){

$beanLink = BeanFactory::getBean('AS01_Invit_asis_grad_UPS', $bean->id);
$beanLink->load_relationship('oe01_oferta_ejecutada_ups_as01_invit_asis_grad_ups_1');

$linked = $bean->oe01_oferta_ejecutada_ups_as01_invit_asis_grad_ups_1->get();

$beanOferta = BeanFactory::getBean('OE01_Oferta_ejecutada_UPS', $linked[0]);

$relacionesOferta = $beanOferta->get_linked_beans(
                                'oe01_oferta_ejecutada_ups_as01_invit_asis_grad_ups_1',
                                '',
                                '',
                                 0,
                                -1,
                                 0,
                                 'as01_invit_asis_grad_ups.inscripto_a_evento = 1'
);

if($beanOferta->id !== null){
$beanOferta->cantidad_inscripciones = count($relacionesOferta);
$beanOferta->save();
}







        }
}


?>

Its probably to do with this:

after_save
Fired after a record is saved. Note that due to some peculiarities some related modules may not be persisted to the database. The logic hook is fired within the SugarBean classes save method. Some implementing classes may save related beans after this method returns. A notable example of this is the saving of email addresses in Company modules.

You may want to look at “after_relationship_add”, ensure you double check to make sure that you are not performing the same operation twice (Globalvar/sessionvar/etc) and have two hooks one for when the relationship is first added and one for just modifying an already existing relationship.

Mark

Hello! Yes, the after_relationship_add didn’t work for me either, as I needed it to modify the second module when a record from the first module is saved. This only triggered when the relationship was created. I solved it by combining an after_save from the main module and sending a POST request to an entry point that handles everything else.