Logic hook is cycling after save

Hello! I have one issue related with my custom logic hook, and when i save the record on AOS_Contracts, is triggering the logic hook but is cycling it… what could be happening?

I have my file named logic_hooks.php

$hook_version = 1;
$hook_array = Array();
// position, file, function 77
$hook_array[‘after_save’] = Array();
$hook_array[‘after_save’][] = Array(1,
‘afterSaveSaveCotizacionsList’,
‘custom/modules/AOS_Contracts/logic_hooks/after_save_logic_hook.php’,
‘AOS_Contracts_afterSaveHook’,
‘showData’);

and the implementation is in : custom/modules/AOS_Contracts/logic_hooks/after_save_logic_hook.php

class AOS_Contracts_afterSaveHook{
//function showData(&$bean, $event, $arguments){ 
function showData(&$bean, $event, $arguments){ 
    $fecha_de_pago 								= $_REQUEST['fecha_de_pago'];
	//$descripcion 						= $_REQUEST[$key.'descripcion'];
	//$huboacuerdo 						= $_REQUEST[$key.'huboacuerdo'];
	$asignado 						= $_REQUEST['compromiso_responsable'];
	$asignadoId 						= $_REQUEST['compromiso_responsable_id'];
	$monda_c 						= $_REQUEST['monda_c'];
	$valor_c 						= $_REQUEST['valor_c'];


        //$cct->retrieve($id[$qty]);
        //$fecha_de_pago as $key => $fch


if (isset($asignadoId) && !is_null($asignadoId) && 1==1){
for ($i=0; $i <= count($fecha_de_pago); $i++) {
$cct = new AOS_Contracts();
$cct->fecha_pago_c = $fecha_de_pago[$qty];
$cct->user_id_c= $asignadoId[$i];
$cct->description2_c= 'this is the way '.$qty;
$cct->moneda_c= $monda_c[$qty];
$cct->valor_c= $valor_c[$qty];
//$cct->parent_type = ‘Meetings’;
$cct->id_c = $bean->id;
//$cct->deleted = $deleted[$qty];


        $cct->save();
# code...


}
}


}

the trouble is when is triggered, cycling it and generating many records… when only should generate the array size…

You can’t save() inside an after_save hook, you get an infinite cycle.

You have to start your hook with an if checking it is the first time the hook is running, or the second time, caused by the save() call.

There are several ways to do this:

https://sugarclub.sugarcrm.com/dev-club/f/questions-answers/202/how-to-update-a-field-in-an-after_save-logic-hook

  • static field
  • checking fetched_row
  • some field in the bean itself

It might be irrelevant, but why are you using & in the function declaration?

The standard method is

function showData($bean, $event, $arguments)

HI PGR, when I hit the ‘Save’ button, i have a bean, but i created a custom template, this template is able to generate and duplicate dynamic fields, so, for example the field named ‘$fecha_de_pago’ is an array, then when i finish to save the module bean, i’d like generate many records as ‘$fecha_de_pago’ has items and save it on the other table, then te relation could be ‘one to many’ ,

I have this:

        function showData(&$bean, $event, $arguments){ 
        $fecha_de_pago 								= $_REQUEST['fecha_de_pago'];
		//$descripcion 						= $_REQUEST[$key.'descripcion'];
		//$huboacuerdo 						= $_REQUEST[$key.'huboacuerdo'];
		$asignado 						= $_REQUEST['compromiso_responsable'];
		$asignadoId 						= $_REQUEST['compromiso_responsable_id'];
		$monda_c 						= $_REQUEST['monda_c'];
		$valor_c 						= $_REQUEST['valor_c'];          
  

			$qty = intval($_REQUEST['compromiso_Qty']) - 1;
			
				while ($qty >= 0){	
				if (isset($bean->id) && !is_null($bean->id)) {

					$cct = new AOS_Contracts();
					//$uuid = $this->generateUUIDPhp();
					$cct->retrieve($bean->id);
					$cct->fecha_pago_c =  $fecha_de_pago[$qty];
					$cct->user_id_c= $asignadoId[$qty];
					$cct->description2_c= 'this is the way '.$qty;
					$cct->moneda_c= $monda_c[$qty];
					$cct->valor_c= $valor_c[$qty];
					$cct->cotizaciones_c= $bean->id;
					//$cct->parent_type	= 'Meetings';
					//$cct->id_c = $bean->id;
					//$cct->deleted = $deleted[$qty];
					$cct->save();
					$qty--;
				}else{
					$cct = new AOS_Contracts();
					$uuid = $this->generateUUIDPhp();
					//$cct->retrieve($bean->id);
					$cct->fecha_pago_c =  $fecha_de_pago[$qty];
					$cct->user_id_c= $asignadoId[$qty];
					$cct->description2_c= 'this is the way '.$qty;
					$cct->moneda_c= $monda_c[$qty];
					$cct->valor_c= $valor_c[$qty];
					$cct->cotizaciones_c = $bean->id;
					$cct->id_c = $uuid;
					$cct->save();
					$qty--;
				}
				
			}
	}	   

this code continues cycling… :S

You are in Contracts after_save hook.

  1. That code gets triggered when saving a Contract

  2. Your code runs, and creates a new Contract bean

  3. You code saves new Contract bean

  4. That causes it to go back to 1.!

1 Like

mmmm, how can i leave the run process when i finish to loop my ‘dates’ array?

You don’t leave the process.

In step 2., you change to an if

  1. If this is the first time the hook runs, from the UI, then do step 3

else (it is the second or more iteration caused by your own code), do nothing.

Hello, i’m in the same situation,

how I can implement a counter of iterations for use after_save hook?

what do you want to do in after_save hook ?

Save some field value of cases in a db table of another modul, using beans for this operation.

what does your code look like now ?

you can use query to update the other module and Use the Cases Bean for usage to set the data.

1 Like

Thanks, now it work!

Just an other thing, how I can use a logic hook after_save, for execute the file .php,
only when I save a record of a modul and not when I modify the same record?

Ok, nothing.
I’ve used the before_seve for this work.

You can use something like

if($bean->id != ''){
     //proced...
}