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...

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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.
)

  • You must add the relationship in the after save hook by an additional "$bean->$relationship->add($related_id)" call not by setting local variables of the bean.

    A relationship can only be added when both beans are already stored in the database.

  • 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.

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <?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);
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX