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:
- Creo registro de modulo Y.
- Creo registro del modulo X.
- 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:
- Creo registro modulo Y.
- 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:
- I create a record in module Y.
- I create a record in module X.
- 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:
- I create a record in module Y.
- 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();
}
}
}
?>