See value of fetched_row inside controller.php

Is there a way to see the field changes inside the controller?

For example for the price field the 2 below are always identical, it always shows the latest value

$this->bean->fetched_row[“price_c”]
$this->bean->price_c

I know it works in logic hooks, but is there a way to compare before and after values inside the controller?

Check here for some ideas:

https://stackoverflow.com/questions/26715403/accessing-sugarcrm-global-variable-current-user-from-a-custom-javascript-file-v

AlxGr

1 Like

Thank you, currently without JS I just use the following function to query the audit table. So you would need to make sure the field is auditable. It’s the equivalent of getting the fetched_row value


// Will fetch last price in audit table, if no price was previously set then return false
function get_last_price($tbl, $id) {

    $query = 'SELECT before_value_string FROM '.$tbl.' WHERE field_name="price_c" AND parent_id="'.$id.'" ORDER BY date_created DESC LIMIT 1;';

    if($result = $GLOBALS["db"]->query($query)) {

        // If price didn't change return false
        if($result->num_rows == 0) {
            return false;
        }
        elseif($row = $result->fetch_assoc()) {
            return $row["before_value_string"];
        }

    }

} 

And I also use it inside the following function just to check if the price has changed or not


// If price has changed return true
function price_change_controller($tbl, $bean) {

    // Current price
    $current_price = $bean->price_c;

    // If we were able to fetch a price from audit table then compare new price vs old price
    if(($last_price = get_last_price($tbl, $bean->id)) !== false) {
        if($current_price != $last_price)
            return true;
    }

    // Check if the price is not NULL, if not return true, because it means the price went from nothing to something
    if(price_is_set($bean))
        return true;


    // If none of the above worked return false
    return false;

}
1 Like