Can't get Lead's related SecurityGroups via Bean in an after_save hook.

Hello everyone!

I’m trying to implement an after_save hook at the Leads module, to assign a new record to a sales rep according to my organization’s rules. I’ve correctly setup the hook code and I can confirm that it is runs when it has to, since the handler writes into the log and everything is shown as expected.

However, I’m experiencing problems when I try to get the record’s related Security Group via the bean object. If the lead has been created with an explicitly set assigned_user, then the bean will load all the securitygroups as it should. However if no user is assigned (which is the use case I’m trying to implement), no securitygroup is loaded even though they were manually set (and show correctly in the UI after clicking “save”).

In terms of functionality, it is critical because the real use case I’m implementing is to handle the Leads that are created from the REST API by other systems/channels of the sales process.

A reduced version of the Hook handler looks as follows:

class LeadAfterSaveHandler {

    function assignSalesRep($leadBean, $event, $arguments) {
        $log = $GLOBALS['log']; 
        $log->warn("LeadAfterSaveHandler->assignSalesRep() - HERE"); // shows up on my log.
        $linkedSecurityGroups = $leadBean->SecurityGroups->get();

         * The following loads data if-and-only-if a user was manually assigned to the record when creating it. 
         * Otherwise, no data is loaded (even when SG where set via the UI).
        $log->warn("count(linkedSecurityGroups): " . count($linkedSecurityGroups));
        $log->warn("linkedSecurityGroups: " . print_r($linkedSecurityGroups, true));

My environment setup is as follows:

SuiteCRM Version: 7.11.6
PHP Version: 7.0.33-0+deb9u3
Apache Version: Apache/2.4.25 (Debian)
DB: 10.0.35-MariaDB-1~xenial

Any help is greatly appreciated in beforehand.

For anyone looking into this, what I ended up doing is re-retrieving the bean from the DB. That solved it and everything seems to be working as expected. In terms of code, the following line at the beginning of my method did it:

$leadBean = BeanFactory::getBean("Leads", $leadBean->id);