LogicHooks before_save create new record

i need some help regarding my cutom logichooks inside my custom mobule(before_save).

It retrieves some data before save, saving the updated record and in certain conditions it creates some additional record in the same module.

This is a short logic:

some custom functions before…

switch count(my_array)

$GLOBALS[‘log’]->FATAL(‘No Data to Retrieve…nothing to do…’);
$bean->order_derpcode = $data[“envcode_destination”];
$bean->shipping_address_street = $data[“address_destination”];
$bean->shipping_address_postalcode = $data[“zip_destination”];
$bean->shipping_address_city = $data[“city_destination”];
$bean->shipping_address_state = $data[“state_destination”];
$bean->shipping_address_country = $data[“country_destination”];
$GLOBALS[‘log’]->FATAL(‘TODO function to create multiple records in teh same module bases on number of array count…’);
$bean = new SameCUstomModule();

When array count is 1 or 0 all is working well however when the default condition occurs an error showing that previous declarations cannot be
redeclared again…

PHP Notice: Undefined property: DBGIT_ManageOrders::$order_snumber_c in /var/www/html/SuiteCRM-7.6.4/custom/modules/DBGIT_ManageOrders/php/update_order.php on line 12, referer: https://myhost/index.php?module=DBGIT_ManageOrders&offset=1&stamp=1466673171048631400&return_module=DBGIT_ManageOrders&action=EditView&record=1a091a2c-e945-d293-31a9-574848b1d899
[Thu Jun 23 11:15:40.598341 2016] [:error] [pid 9936] [client IP:58983] PHP Fatal error: Cannot redeclare convertDate() (previously declared in /var/www/html/SuiteCRM-7.5.2/custom/modules/DBGIT_ManageOrders/php/update_order.php:17) in /var/www/html/SuiteCRM-7.5.2/custom/modules/DBGIT_ManageOrders/php/update_order.php on line 17, referer: https://myhost/index.php?module=DBGIT_ManageOrders&offset=1&stamp=1466673171048631400&return_module=DBGIT_ManageOrders&action=EditView&record=1a091a2c-e945-d293-31a9-574848b1d899

and so on…
Could anyone point me in teh right way?

Thank you

don’t use $bean for your new SameCUstomModule();, but likely your error is that you have 2 classes with the same name or are doing a include or require instead of include_once or require_once somewhere

mm…i don’t know because the php script fired by logic hook is inside the module…so i don’t need to include anything…

anyway…i’ve change the name into

$newOrder = new DBGIT_ManageOrders();

seems working but i see 2 things:

-> its quite slow…normally it creates 2 or 3 additional records(with only record name saved as for test)…there’s no reason to take much time…compared to case(1): which is really fast
-> some additional errors is coming…just an example:
PHP Notice: Undefined property: User::$project_users_1 in /var/www/html/SuiteCRM-7.5.2/modules/AOS_PDF_Templates/templateParser.php on line 71, referer: https://myhost/index.php?module=DBGIT_ManageOrders&offset=1&stamp=1466713320015191900&return_module=DBGIT_ManageOrders&action=EditView&record=1a091a2c-e945-d293-31a9-574848b1d899
[Thu Jun 23 22:22:08.905901 2016] [:error] [pid 3150] [client] PHP Notice: Undefined property: User::$SecurityGroups in /var/www/html/SuiteCRM-7.5.2/modules/AOS_PDF_Templates/templateParser.php on line 71, referer: https://myhost/index.php?module=DBGIT_ManageOrders&offset=1&stamp=1466713320015191900&return_module=DBGIT_ManageOrders&action=EditView&record=1a091a2c-e945-d293-31a9-574848b1d899

and some others regarding property: cc, bcc and so on while in case(1) doesn’t appear…

Any help?

Thank you


For me you go in “infinite loop”.

Case 1 : you only set some bean field with data

Default : you create same custom bean and save … then before_save is fired again !

Easy solution, create bean by SQL.
Maybe another solution, more difficult, create bean after_save with some flag.
Or maybe by Workflow…

mmmh…as you can see i’m doing some loop prevention…

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

// Include Medoo Framework -> original copyright Medoo Framework -> http://medoo.in/ //
require_once 'custom_library/medoo.min.php';

class UpdateOrder {
    // Set static variable to prevent loop in save bean
    static $already_ran = false;
    function DoUpdateerp(&$bean, $event, $arguments) {
        // Check if has been already saved
        if(self::$already_ran == true) return;
        self::$already_ran = true;
        $GLOBALS['log']->fatal('Prevent Loop called for Order: '.$bean->name);
         // Retrieve Bean data
        $OrderId = $bean->id;
        $SNumber = $bean->order_snumber_c;
        $Incoterms = $bean->dbgit_incoterms_id_c;
        // Declare my custom function
        // Convert AS400 Date format(Centuryyearmonthday - 1160101 into a human readable format YY-mm-dd 2016-01-01) 
        function convertDate($wrongDate) {
            $year = substr($wrongDate, 1, 2);
            $month = substr($wrongDate, 3, 2);
            $day = substr($wrongDate, 5, 2);
            $originalDate = $year."-".$month."-".$day;    
            $newDate = date("Y-m-d", strtotime($originalDate));
            //$GLOBALS['log']->FATAL('Original Date: '.$wrongDate);
            //$GLOBALS['log']->FATAL('Converted Date: '.$newDate);
            return $newDate;
        // Initialize MYSQL Database Connection(insert imported data from as400 into mysql)
        $database = new medoo([
        'database_type' => 'mysql',
        'database_name' => 'erpdata',
        'server' => '',
        'username' => 'root',
        'password' => 'sandbgroup',
        'charset' => 'utf8'
        // Execute the query
        $datas = $database->select("table_spedizioni_mare_ita", [
        ],  [
            "AND" => [
            "office_code" => substr($SNumber, 0, 2),
            "year_sped" => substr($SNumber, 2, 4),
            "prog_sped" => substr($SNumber, 6, 6),
        if( is_array( $datas ) ) {
            switch(count($datas)) {
            case 0:
                $GLOBALS['log']->FATAL('No Data to Retrieve...nothing to do....');
            case 1:
                $GLOBALS['log']->FATAL('Only one shipment found....proceed with update....');
                foreach($datas as $data)
                    // Retrieve ERP data from external database(AS400-Bridge)
                    // Rertrieve incoterms array based on rcode provided by ERP
                    $ScmIncoterms = new DBGIT_Incoterms();
                    $ScmIncoterms->retrieve_by_string_fields(array('incoterms_rcode' => $data["incoterms"]));
                    // Rertrieve POL array based on pcode provided by ERP
                    $ScmPol = new DBGIT_Ports();
                    $ScmPol->retrieve_by_string_fields(array('port_pcode' => $data["pol"]));
                     // Rertrieve POD array based on pcode provided by ERP
                    $ScmPod = new DBGIT_Ports();
                    $ScmPod->retrieve_by_string_fields(array('port_pcode' => $data["pod"]));
                     // Rertrieve Carrier array based on Carrier code provided by ERP
                    $ScmMCarrier = new DBGIT_MaritimeCarriers();
                    $ScmMCarrier->retrieve_by_string_fields(array('maritime_code' => $data["carrier"]));
                     // Rertrieve Container type array based on Container code provided by ERP
                    $ScmContainerType = new DBGIT_Containers();
                    $ScmContainerType->retrieve_by_string_fields(array('container_code' => $data["container_type"]));
                    // Rertrieve Package type array based on Package code provided by ERP
                    $ScmPackageType = new DBGIT_Packaging();
                    $ScmPackageType->retrieve_by_string_fields(array('packaging_code' => $data["package_code"]));
                    // Debug Message from log
                    $GLOBALS['log']->FATAL('Before Save .... debug message from UpdateOrder');
                    $GLOBALS['log']->FATAL('OrderID: '.$OrderId);
                    $GLOBALS['log']->FATAL('Shipment Number: '.$SNumber);
                    $GLOBALS['log']->FATAL('Incoterms ID: '.$Incoterms);
                    $GLOBALS['log']->FATAL(substr($SNumber, 0, 2)." - ".substr($SNumber, 2, 4)." - ".substr($SNumber, 6, 6));
                    $GLOBALS['log']->FATAL('Incoterms Name: '.$ScmIncoterms->name);
                    $GLOBALS['log']->FATAL('POL: '.$ScmPol->name);
                    $GLOBALS['log']->FATAL('POD: '.$ScmPod->name);
                    $GLOBALS['log']->FATAL('Routing Direction: '.$data["routing"]);
                    $GLOBALS['log']->FATAL('Service Type: '.$data["type_sped"]);
                    $GLOBALS['log']->FATAL('Marittime Carrier: '.$ScmMCarrier->name);
                    $GLOBALS['log']->FATAL('Container Type: '.$ScmContainerType->name);
                    $GLOBALS['log']->FATAL('Packaging Type: '.$ScmPackageType->name);
                    // Save data into Bean
                    $bean->order_container = $data["container_nr"];
                    $bean->order_pacnum = $data["qta_colli"];
                    $bean->order_goods_desc_c = $data["dett_merce"];
                        // todo: create Unità di misura module to relate t to quantity
                        $bean->order_punit = $data["um_pezzi"];
                        $bean->order_pnum = $data["qta_pezzi"];
                        // end todo
                    $bean->order_weight = $data["peso_lordo"];
                    $bean->order_volume = $data["volume"];
                    $bean->dbgit_incoterms_id_c = $ScmIncoterms->id;
                    $bean->order_routing = $data["routing"];
                    $bean->order_service = $data["type_sped"];
                    $bean->dbgit_ports_id_c = $ScmPol->id;
                    $bean->dbgit_ports_id1_c = $ScmPod->id;
                    $bean->dbgit_maritimecarriers_id_c = $ScmMCarrier->id;
                    $bean->dbgit_containers_id_c = $ScmContainerType->id;
                    $bean->dbgit_packaging_id_c = $ScmPackageType->id;
                    $bean->order_bookingn = $data["booking_nr"];        
                    $bean->order_etd = convertDate($data["etd"]);
                    $bean->order_eta = convertDate($data["eta"]);
                    $bean->order_oaccountname = $data["accountname_origin"];
                    $bean->order_oerpcode = $data["envcode_origin"];
                    $bean->billing_address_street = $data["address_origin"];
                    $bean->billing_address_postalcode = $data["zip_origin"];
                    $bean->billing_address_city = $data["city_origin"];
                    $bean->billing_address_state = $data["state_origin"];
                    $bean->billing_address_country = $data["country_origin"];
                    $bean->order_daccountname = $data["accountname_destination"];
                    $bean->order_derpcode = $data["envcode_destination"];
                    $bean->shipping_address_street = $data["address_destination"];
                    $bean->shipping_address_postalcode = $data["zip_destination"];
                    $bean->shipping_address_city = $data["city_destination"];
                    $bean->shipping_address_state = $data["state_destination"];
                    $bean->shipping_address_country = $data["country_destination"];
                    $newOrder->inSync = true;
                $GLOBALS['log']->FATAL('TODO function to create multiple orders base on multiple shipments...');
                $GLOBALS['log']->FATAL('OrderID: '.$OrderId);
                $GLOBALS['log']->FATAL('Shipment Number: '.$SNumber);
                $GLOBALS['log']->FATAL('Incoterms ID: '.$Incoterms);
                $newOrder = new DBGIT_ManageOrders();

            } // EOF Switch      
        } // EOF IF STATEMENT  
    } // EOF DoUpdateerp Function
} // EOF UpdateOrder Class

What do u you exactly mean with “create bean by SQL”…you mean about create records directly to database instead of using bean?

Do you see any discrepancies in my code?

Thank you