Let the platform do the work

Comparing Bean Properties Between Logic Hooks

Overview

How to compare the properties of a bean between the before_save and after_save logic hooks

Storing and Comparing Values

When working with a bean in the after_save logic hook, you may notice that the after_save fetched rows no longer match the before_save fetched rows. If your after_save logic needs to be able to compare values that were in the before_save, you can use the following example to help you store and use the values.

./custom/modules/<module>/logic_hooks.php

  <?php

$hook_version = 1;
$hook_array = Array();

$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(
    1,
    'Store values',
    'custom/modules/<module>/My_Logic_Hooks.php',
    'My_Logic_Hooks',
    'before_save_method'
);

$hook_array['after_save'] = Array();
$hook_array['after_save'][] = Array(
    1,
    'Retrieve and compare values',
    'custom/modules/<module>/My_Logic_Hooks.php',
    'My_Logic_Hooks',
    'after_save_method'
);

?>

./custom/modules/<module>/My_Logic_Hooks.php

  <?php

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

class My_Logic_Hooks
{
    function before_save_method($bean, $event, $arguments)
    {
        //store as a new bean property
        $bean->stored_fetched_row_c = $bean->fetched_row;
    }

    function after_save_method($bean, $event, $arguments)
    {
        //check if a fields value has changed
        if (
            isset($bean->stored_fetched_row_c)
            && $bean->stored_fetched_row_c['field'] != $bean->field
           )
        {
            //execute logic
        }
    }
}

?>