Is it possible to overwrite calculated fields?

Hi, 

I am wondering - is it possible to overwrite calculated fields? 

I have a calculated field (Total Contract Value) which is calculated based on ARR and subscription duration. But there are some exceptions to the formula. Rather than altering the formula, is it possible to edit the field, so that I can overwrite the calculated value and input it manually?

Thanks!

Ada

  • Hello

    A possible way to do this is to migrate the logic that you currently have in the calculated formula into a SetValue Dependency 
    This will allow you to decide your desired rules to set the field read-only/calculated.

    For example, you could use a checkbox field to allow the user to enable the contract value to be overridden, something like this: 

    <?php
    
    //path custom/Extension/modules/Opportunities/Ext/Dependencies/handle_contract_value.php
    
    $dependencies['Opportunities']['handle_contract_value'] = array(
        'hooks' => array("edit", "view"),
        'trigger' => 'true',
        'triggerFields' => array('override_value_manually_c'),
        'onload' => true,
        'actions' => array(
            array(
                'name' => 'SetValue',
                'params' => array(
                    'target' => 'total_contract_value_c',
                    'value' => 'ifElse(equal($override_value_manually_c,false),<yourCurrentFormulaHere>,$total_contract_value_c)'
                )
            ),
            array(
                'name' => 'ReadOnly',
                'params' => array(
                    'target' => 'total_contract_value_c',
                    'value' => 'equal($override_value_manually_c,false)', 
                ),
            )
        )
    );


    I hope this helps. 

    André

  • Hi ,

    The solution proposed by is, from my perspective, the recommended path to ensure entered data is retained. There is one alternative that has some more destructive qualities that may be acceptable or even desirable depending on your use case. When creating a calculated field in Studio, a file is created with the calculation properties at:

    ./custom/Extension/modules/{module_name}/Ext/Vardefs/sugarfield_{field_name}.php

    In that file, there is a parameter:

    $dictionary['Opportunity']['fields']['{field_name}']['enforced']='1';

    If you change that value to 0 and then perform a quick repair, the field will have the following qualities:

    1. It is no longer read-only. It can be edited at any time.
    2. The calculation will trigger as you have it defined
    3. A user can overwrite the calculated value
    4. If any of the dependencies in the fields calculation are updated after a user-entered value is saved, the calculation will re-trigger and overwrite the user-entered value

    Chris