before_save logic hook runs multiple times (not infinite)

My before_save logic hook runs multiple times. How do only run this logic hook once? The following methods do not work. I have tried them both.

Prevent a Logic Hook From Running Multiple Times 

http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_6.5/Module_Framework/Logic_Hooks/Example… 

I have a before_save logic hook on "Leads" that should only run for new records. But it is being ran more than once for new leads. This hook does not run on normal updates.. 

I do have 1 "Process Definition" on "Target Module Leads" that "Applies to new records" that creates a "call record" for for New Leads only! 

I have a second "Process Definition" on "Target Module Leads" that "Applies to updated records only" that changes a field value based on some conditions. 

I have a third "Process Definition" on "Target Module Calls" that "Applies to updated records only" that changes some fields on "Leads" based on some conditions.

Here is my sample code:

<?php

if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class MapCBSA
{

   function mapCBSA($bean, $event, $arguments)
   {
      if (isset($bean->fetched_row['id'])) { return; }

      if(!isset($bean->ignore_cbsa_logic_hook) || $bean->ignore_cbsa_logic_hook === false) {

         _ppl('This has ran!');

         $bean->ignore_cbsa_logic_hook = true;

      }   

}

In my sugar.log I see this ran multiple times

Thu Jun 22 18:44:44 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() output start -----------------------------
Thu Jun 22 18:44:44 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] This has ran!
Thu Jun 22 18:44:44 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() output end -----------------------------
Thu Jun 22 18:44:44 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() file: /vagrant/custom/modules/Leads/lead_before_save_logic_hook.php line#: 19-----------------------------
Thu Jun 22 18:44:44 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() output start -----------------------------
Thu Jun 22 18:44:44 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] This has ran!
Thu Jun 22 18:44:44 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() output end -----------------------------
Thu Jun 22 18:44:44 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() file: /vagrant/custom/modules/Leads/lead_before_save_logic_hook.php line#: 19-----------------------------
Thu Jun 22 18:44:45 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] call save
Thu Jun 22 18:44:45 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] call save
Thu Jun 22 18:44:46 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() output start -----------------------------
Thu Jun 22 18:44:46 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] This has ran!
Thu Jun 22 18:44:46 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() output end -----------------------------
Thu Jun 22 18:44:46 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() file: /vagrant/custom/modules/Leads/lead_before_save_logic_hook.php line#: 19-----------------------------
Thu Jun 22 18:44:46 2017 [7218][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] call save

When I disable all the process definitions I still get this in the logs:

Thu Jun 22 18:52:43 2017 [8175][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() output start -----------------------------
Thu Jun 22 18:52:43 2017 [8175][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] This has ran!
Thu Jun 22 18:52:43 2017 [8175][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() output end -----------------------------
Thu Jun 22 18:52:43 2017 [8175][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() file: /vagrant/custom/modules/Leads/lead_before_save_logic_hook.php line#: 19-----------------------------
Thu Jun 22 18:52:43 2017 [8175][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() output start -----------------------------
Thu Jun 22 18:52:43 2017 [8175][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] This has ran!
Thu Jun 22 18:52:43 2017 [8175][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() output end -----------------------------
Thu Jun 22 18:52:43 2017 [8175][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() file: /vagrant/custom/modules/Leads/lead_before_save_logic_hook.php line#: 19-----------------------------

HELP!!!! How do i get this logic_hook to only run a single time. 

  • Ok, I have found the solution. http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.9/Data_Framework/Models/SugarBean/ 

    Since this is a before_save logic hook we need to change some code:

    Change: 

    if (isset($bean->fetched_row['id'])) { return; }

    TO: 

    if (!isset($arguments['isUpdate']) || $arguments['isUpdate'] == true) { return; }

    This will cause the output to only be once 

    Fri Jun 23 13:39:12 2017 [8331][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() output start -----------------------------
    Fri Jun 23 13:39:12 2017 [8331][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] This has ran!
    Fri Jun 23 13:39:12 2017 [8331][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() output end -----------------------------
    Fri Jun 23 13:39:12 2017 [8331][c22731cc-3bdf-11e7-9825-02135a7c6b1f][FATAL] ------------------------------ _ppLogger() file: /vagrant/custom/modules/Leads/lead_before_save_logic_hook.php line#: 19-----------------------------