Robert Sinclair
Robert Sinclair

Reputation: 5426

assigned_user_id doesn't change

Gotta be something simple I'm sure, but I'm just not seeing it.

I have a before and after save hooks which simply check if the assigned user ID has changed. But I'm getting the same user_ID in the before AND the after save hooks.

Logic Hook registry

$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(
 77,
 'If a new user is assigned to the unit of inventory it changes inventory status to OFFER',
 'custom/modules/un_inventory/user_assigned_c.php',
 'user_assigned_c',
 'user_assigned_before_save');

$hook_array['after_save'] = Array();
$hook_array['after_save'][] = Array(
 78,
 'If a new user is assigned to the unit of inventory it changes inventory status to OFFER',
 'custom/modules/un_inventory/user_assigned_c.php',
 'user_assigned_c',
 'user_assigned_after_save');

And the logic hook

class user_assigned_c {

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

        $GLOBALS['log']->info('User ID before save: '.$bean->assigned_user_id);

    }

    function user_assigned_after_save($bean, $event, $arguments) {
        // $bean->assigned_user_id stays the same as in the method above...
        $GLOBALS['log']->info('User ID after save: '.$bean->assigned_user_id);
    }

}

Upvotes: 1

Views: 401

Answers (2)

Jay
Jay

Reputation: 3950

In SugarCRM 7+ you can use an after_save hook:

   function user_assigned_after_save($bean, $event, $arguments) {
        if (!$arguments['isUpdate']) {
            $GLOBALS['log']->info('New bean with User ID: '.$bean->assigned_user_id);
        } else if (isset($arguments['dataChanges']['assigned_user_id'])) {
            $idChange = $arguments['dataChanges']['assigned_user_id'];
            $old = $idChange['before'];
            $new = $idChange['after'];
            $GLOBALS['log']->info("Bean's User ID was changed from '$old' to '$new'");
        }
    }

In SugarCRM (old and new versions), also SuiteCRM,
you can use a before_save hook (as stated by Jim):

   function user_assigned_before_save($bean, $event, $arguments) {
        $old = $bean->fetched_row['assigned_user_id'];
        $new = $bean->assigned_user_id;
        if ($old != $new) {
            $GLOBALS['log']->info("Bean's User ID was changed from '$old' to '$new'");
        }
    }

If you use the before_save hook and you have hooks that modify the assigned_user_id field, make sure that those hooks run in the right order (First parameter in hook array of each hook defines order).

Upvotes: 1

Jim
Jim

Reputation: 22656

This is expected. A usual flow might be the following:

$bean = getBeanFromSomewhere();
$bean->assigned_user_is = "12345ABC";
$bean->save();

Note that, in the save the assigned user id is already set.

If you want to check what the previous value of something is in a hook you can use the fetched_row attribute - this stores a list of the fields as they were when the bean was loaded.

I.e.

$newId = $bean->assigned_user_id;
$oldId = $bean->fetched_row['assigned_user_id'];
if($newId !== $oldId){
    //Assigned user changed!
}

I believe there was issues in the past with fetched_row in an after save hook but hopefully the above helps.

Upvotes: 4

Related Questions