calculated field working only in detail view

Hi,

I am new user of SuiteCRM.

I have followed https://suitecrm.com/forum/suitecrm-7-0-discussion/3284-creating-a-calculated-field-to-count-days-until-a-date?limitstart=0 to create a “weighted_amount” field for an opportunity, which would be equal to amount * probability (and am surprised it does not already exist, by the way - did I miss something?)

The field works great in the detail view, but shows as “€0.00” in the list view, and don’t understand why. I have used the Repair > Quick repair and rebuild but it does not help.

Also what is strange is that despite the field is a varchar it appears as a Currency (well, at some point in time, I changed to Currency in the code, but went back to varchar afterwards as I had issues to fix, but I don’t understand where the memory of this change could be stored, if it is stored somewhere)

Could you please advise? you will find below my code. I have used Studio to change the layouts in order to add the field in the detail view and list view. And by the way, had to add the “studio” => “visible” key in the PHP code, which mysteriously disappears once I repair and rebuild. Not sure to use the right approach, please correct me on the right way to do as well, I could not find documentation other than the SugarCRM one.

Many thanks in advance, and best regards


 cat Ext/Vardefs/vardefs.ext.php
<?php
 //WARNING: The contents of this file are auto-generated


 // created: 2016-05-28 02:01:16
$dictionary['Opportunity']['fields']['jjwg_maps_lng_c']['inline_edit']=1;



 // created: 2016-05-28 02:01:16
$dictionary['Opportunity']['fields']['jjwg_maps_lat_c']['inline_edit']=1;



 // created: 2016-05-28 02:01:16
$dictionary['Opportunity']['fields']['jjwg_maps_geocode_status_c']['inline_edit']=1;



 // created: 2016-05-28 02:01:17
$dictionary['Opportunity']['fields']['jjwg_maps_address_c']['inline_edit']=1;




$dictionary['Opportunity']['fields']['weighted_amount'] = array(
        'name' => 'weighted_amount',
        'vname' => 'LBL_WEIGHTED_AMOUNT',
        'type' => 'varchar',
        'len' => '10',
        'source' => 'non-db',
);

------

 cat logic_hooks.php
<?php
// Do not store anything in this file that is not part of the array or the hook version.  This file will
// be automatically rebuilt in the future.
 $hook_version = 1;
$hook_array = Array();
// position, file, function
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1, 'Opportunities push feed', 'modules/Opportunities/SugarFeeds/OppFeed.php','OppFeed', 'pushFeed');
$hook_array['before_save'][] = Array(77, 'updateGeocodeInfo', 'modules/Opportunities/OpportunitiesJjwg_MapsLogicHook.php','OpportunitiesJjwg_MapsLogicHook', 'updateGeocodeInfo');
$hook_array['after_save'] = Array();
$hook_array['after_save'][] = Array(77, 'updateRelatedMeetingsGeocodeInfo', 'modules/Opportunities/OpportunitiesJjwg_MapsLogicHook.php','OpportunitiesJjwg_MapsLogicHook', 'updateRelatedMeetingsGeocodeInfo');
$hook_array['after_save'][] = Array(78, 'updateRelatedProjectGeocodeInfo', 'modules/Opportunities/OpportunitiesJjwg_MapsLogicHook.php','OpportunitiesJjwg_MapsLogicHook', 'updateRelatedProjectGeocodeInfo');
$hook_array['after_relationship_add'] = Array();
$hook_array['after_relationship_add'][] = Array(77, 'addRelationship', 'modules/Opportunities/OpportunitiesJjwg_MapsLogicHook.php','OpportunitiesJjwg_MapsLogicHook', 'addRelationship');
$hook_array['after_relationship_delete'] = Array();
$hook_array['after_relationship_delete'][] = Array(77, 'deleteRelationship', 'modules/Opportunities/OpportunitiesJjwg_MapsLogicHook.php','OpportunitiesJjwg_MapsLogicHook', 'deleteRelationship');


$hook_array['after_retrieve'] = Array();
$hook_array['after_retrieve'][] = Array(1, 'Weighted amount = amount * proba', 'custom/modules/Opportunities/OpportunitiesWeightedAmountLogicHook.php', 'OpportunitiesWeightedAmountLogicHook', 'after_retrieve');

$hook_array['process_record'] = Array();
$hook_array['process_record'][] = Array(1, 'Weighted amount = amount * proba', 'custom/modules/Opportunities/OpportunitiesWeightedAmountLogicHook.php', 'OpportunitiesWeightedAmountLogicHook', 'process_record');
?>

----------

 cat OpportunitiesWeightedAmountLogicHook.php
<?php

if (!defined('sugarEntry') || !sugarEntry) die ('Not a valid Entry Point');

class OpportunitiesWeightedAmountLogicHook {
        function after_retrieve ($bean, $event, $arguments) {
                $this->compute_weighted_amount ($bean);
        }
        function process_record ($bean, $event, $arguments) {
                $this->compute_weighted_amount ($bean);
        }
        function compute_weighted_amount ($bean, $event, $arguments) {
                        $focus = $bean->custom_fields->retrieve();
                        $amount = $bean->amount;
                        $prob = $bean->probability / 100.0;
                        $bean->weighted_amount = $amount * $prob;
        }
}

?>


Just if it may help people having the same issues:

  • the strange things happening randomly were due to the fact that I was putting my files in the wrong directory: they had to be in the Extension directories, and I was putting them in the place they are written by the “Quick build an repair” feature. So my chagnes were overwritten.

  • to make the field computed in the list view, I had to use a second logic hook, which is “process_record” in addition to the after_retrieve. Also I had to fetch the input fields by using the retrieve method of the SugarBean. by the way I find it weird that after_retrieve is not called for a list view if it is called for a detail view!?

Finally as I wanted my calculated field to appear in a report, and since reports do not accept non-db fields, I made my field persistent, and computed it in the before_save logic hook rather than after_retreive and process_record…

Hope this helps

Read the SugarCRM documentation regarding logic_hooks http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_6.5/Module_Framework/Logic_Hooks/Module_Hooks/ after_retrieve and process_record are differentiated because of many reasons, for example you wouldn’t want to make big changes in a list view(process_record) of 100 records, you would like to handle them individually(after_retrieve).

best regards