AnsweredAssumed Answered

Logic Hook after_relationship_add: MySQL error 1062: Duplicate entry

Question asked by Pedro Bazan on Jun 4, 2014
Latest reply on Jun 4, 2014 by Pedro Bazan
Hi.

I have made a Logic Hook on a Custom Module (Credit request), One Account has many of Credit request but only one might to be Active. Credit request modules has a "status" field with values "New", "Active", "Inactive".

When a new Credit request is created this has a status "New", the "logic hook" works on "after_relationship_add" event, looks for  the "Active" records and update their status field to "Inactive",later the logic hook try to update the New record's status field to "Active".

In this moment Sugar goes to error. In the log file appears the next message:

Wed Jun  4 14:41:59 2014 [3572][1][FATAL]  Query Failed: INSERT INTO cmx02_solicitudescredito_cstm (id_c ,status_c ) VALUES ('57069d6d-40c9-bf0a-9ca3-538f9233ef41' ,'Activa' ): MySQL error 1062: Duplicate entry '57069d6d-40c9-bf0a-9ca3-538f9233ef41' for key 'PRIMARY'
Wed Jun  4 14:41:59 2014 [3572][1][FATAL] Error inserting into table: cmx02_solicitudescredito: Query Failed: INSERT INTO cmx02_solicitudescredito (id,name,date_entered,date_modified,modified_user_id,created_by,deleted,team_id,team_set_id,assigned_user_id,limite_credito_autorizado,currency_id,base_rate,documentacion_completa,fecha_aprobado_rechazado,condiciones_venta_autorizadas,instrumento_pago,tipo_modena,taza_fiscal,solicitud_completa_lista,autorizacion_de_gerente,folio_solicitud,autorizacion_credito_cobranza,dia_revision,dias_pago,credito_maximo_sugerido,neto_a_dias)
     VALUES ('57069d6d-40c9-bf0a-9ca3-538f9233ef41','20140604 Merxbp','2014-06-04 21:41:59','2014-06-04 21:41:59','1','1',0,'1','1','1',100000,'-99',1,1,'2014-06-07','2','Pago_en_efectivo','EUR','VC11',1,1,1234567,1,'1','^2^,^1^',1223467,'15'): MySQL error 1062: Duplicate entry '57069d6d-40c9-bf0a-9ca3-538f9233ef41' for key 'PRIMARY'

The code of my Logic Hook is the next:

class CMX02_SolicitudesCreditoLogicHookCustom{   public function desactivaSolicitudesPorNueva($bean, $event, $arguments){    global $app_list_strings;    if($arguments['related_module'] === 'Accounts'){     $account_bean = BeanFactory::getBean($arguments['related_module'], $arguments['related_id']);     $account_bean->load_relationship('cmx02_solicitudescredito_accounts');     $solicitudes = $account_bean->cmx02_solicitudescredito_accounts->getBeans();     foreach ($solicitudes as $solicitud_bean) {      if($solicitud_bean->status_c === 'Activa'){       $solicitud_bean->status_c = 'Inactiva';       $solicitud_bean->save();      }     }        $bean->status_c = 'Activa';     $bean->save();    }     }  }
What is my mistake?

Outcomes