Logic Hook uses previous relationship value when firing

I have a simple logic hook that changes the commission amount based on the user selected in the field selling_agent_c:

class sellingcommission {

function sellingcommissionfield($bean, $event, $arguments) {

    if ($bean->selling_agent_c == "Will") {
    
                $bean->selling_agent_comm_c = 10;     
    
            } else if ($bean->selling_agent_c == "Robert")  {
    
                $bean->selling_agent_comm_c = 20;     
    
            }
    }

}

The hook fires, however it uses the previous user that was in the relation field “selling_agent_one.”

For example if the previous user selected was Rob and I change it to Will and click save, then the value will be 20 instead of 10.

If I save the record again, then the right value appears. It works fine when a record is created for the first time. The issue happens when I edit the record again.

It seems that the new selected user has not been updated in the DB, when the logic hook runs. The previous value is there instead and is used.

I’ve tried using an after_save and before_save, but still getting the same results.

Any ideas? Any help would be much appreciated.

What kind of logic hook did you implement? before_save or after_save?

Regards

Hi andopes,

Thanks for the reply. Have tried both before_save and after_save.

With the after_save version, I included $bean->save() at the end of the above function, but it still had the same issue. Function works fine, but still uses the previous value in the relate field… which is just bizarre.

Code used for after_save, which I thought would have fixed the issue.

$hook_array['after_save'][] = array(

        10,

        'Selling Commission',

        'custom/modules/APGRP_Sale_of_Business_Pharmacy/Ext/LogicHooks/Custom_Logic_Hooks.php',

        'sellingcommission',

        'sellingcommissionfield'

    );
class sellingcommission {

function sellingcommissionfield($bean, $event, $arguments) {

    if ($bean->selling_agent_c == "Will") {
    
                $bean->selling_agent_comm_c = 10; 
                $bean->save();
    
            } else if ($bean->selling_agent_c == "Robert")  {
    
                $bean->selling_agent_comm_c = 20;
               $bean->save();
    
            }
    }

}

It is a custom module. I’m not sure of that makes a difference.