Relate Field is updated with Logic Hook according to log, but is not actually updated

I'm trying to update the relate field in in bene_beneficiaries with what is in the relate field on the related accts_financial_accounts record. The log is showing that the values I want in the field are there both at the end of the before_save hook and in the after_save hook. However the field I want updated on the bene_beneficiaries record is not updated.

What could be the problem?

This...

<?php

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

class PopulateFieldsLogicHook
{
public function populateFields($bean, $event, $arguments)
{


// Load the related accts_Financial_Accounts record
$bean->load_relationship('bene_beneficiaries_accts_financial_accounts');
$relatedAccounts = $bean->bene_beneficiaries_accts_financial_accounts->getBeans();
$financialAccountBean = array_shift($relatedAccounts);

if ($financialAccountBean) {
// Set the bene_Beneficiaries fields based on the related accts_Financial_Accounts record
$bean->account_registration_type = $financialAccountBean->account_registration_type_c;
$bean->bene_beneficiaries_accountsaccounts_ida = $financialAccountBean->accts_financial_accounts_accountsaccounts_ida;
$bean->bene_beneficiaries_accounts_name = $financialAccountBean->accts_financial_accounts_accounts_name;
}
$GLOBALS['log']->fatal("Field values before save: " . print_r([
'bene_beneficiaries_accountsaccounts_ida' => $bean->bene_beneficiaries_accountsaccounts_ida,
'bene_beneficiaries_accounts_name' => $bean->bene_beneficiaries_accounts_name,
], true));

}

function afterSaveMethod($bean, $event, $arguments)
{
// Log the values after the save operation
$GLOBALS['log']->fatal("Field values after save: " . print_r([
'bene_beneficiaries_accountsaccounts_ida' => $bean->bene_beneficiaries_accountsaccounts_ida,
'bene_beneficiaries_accounts_name' => $bean->bene_beneficiaries_accounts_name,
], true));
}
}
?>

Returns this in the log...

Mon Oct 30 19:54:32 2023 [3950][1][FATAL] Field values before save: Array
(
[bene_beneficiaries_accountsaccounts_ida] => e400f6xx-7893-11e8-a344-02fd0a47dd48
[bene_beneficiaries_accounts_name] => Zxxx, Christine B.
)

Mon Oct 30 19:54:32 2023 [3950][1][FATAL] Field values after save: Array
(
[bene_beneficiaries_accountsaccounts_ida] => e400f6xx-7893-11e8-a344-02fd0a47dd48
[bene_beneficiaries_accounts_name] => Zxxx, Christine B.
)

Parents Reply Children
  • Thank you, Harald. I added that to the before_save hook an removed the after_save hook. It works great. For completeness, here is my final code.

    <?php
    
    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    
    class PopulateFieldsLogicHook
    {
        public function populateFields($bean, $event, $arguments)
        {
            // Load the related accts_Financial_Accounts record
            $bean->load_relationship('bene_beneficiaries_accts_financial_accounts');
            $relatedAccounts = $bean->bene_beneficiaries_accts_financial_accounts->getBeans();
            $financialAccountBean = array_shift($relatedAccounts);
    		
    		$bean->account_registration_type = $financialAccountBean->account_registration_type_c;
    		
            if ($financialAccountBean && $bean->bene_beneficiaries_accountsaccounts_ida != $financialAccountBean->accts_financial_accounts_accountsaccounts_ida) {
                // Set the bene_Beneficiaries fields based on the related accts_Financial_Accounts record
    			$bean->bene_beneficiaries_accountsaccounts_ida = $financialAccountBean->accts_financial_accounts_accountsaccounts_ida;
    			$bean->load_relationship('bene_beneficiaries_accounts');
    			$bean->bene_beneficiaries_accounts->add($bean->bene_beneficiaries_accountsaccounts_ida);
            }
        }
    }
    ?>