Read-Only currency field with currency

Hi @all

In my opportunities, I’d like to set certain fields to read-only, since their value is set by a workflow in the background. Example: The opportunity has a field TCV. Based on the probability the workflow sets the value of the “read-only” field “TCV weighted” to a certain amount. Since the user does not need to change the value of the field, it is in read-only mode using:

(file: custom/Extension/modules/Opportunities/Ext/Vardefs/ _override_sugarfield_opt_tcv_weighted_c.php)

$dictionary[‘Opportunity’][‘fields’][‘opt_tcv_weighted_c’][‘inline_edit’]=‘’;
$dictionary[‘Opportunity’][‘fields’][‘opt_tcv_weighted_c’][‘options’]=‘numeric_range_search_dom’;
$dictionary[‘Opportunity’][‘fields’][‘opt_tcv_weighted_c’][‘labelValue’]=‘TCV weighted’;
$dictionary[‘Opportunity’][‘fields’][‘opt_tcv_weighted_c’][‘enable_range_search’]=‘1’;
$dictionary[‘Opportunity’][‘fields’][‘opt_tcv_weighted_c’] = array(
‘type’ => ‘readonly’,
‘studio’ => ‘visible’,
);

So far so good and it works fine. Now, I noticed a strange behavior. In the list view, the amount is displayed with the currency symbol and the correct number of digits after the decimal point. When I open the detail view, the value is shown with 6 zeros after the decimal point and the currency symbol is missing (see screenshots).

listview

I was playing around with the option “currency => true” but without luck. Any ideas how to show the currency properly with read-only fields?

I am using SuiteCRM 8.7.1 with PHP 8.1.

Cheers,
Carsten

Can you please set type of the field as currency and readonly to true and see if works as expected

$dictionary[‘Opportunity’][‘fields’][‘opt_tcv_weighted_c’][‘type’]=‘currency’;
$dictionary[‘Opportunity’][‘fields’][‘opt_tcv_weighted_c’][‘readonly’]= true;
1 Like

Hi @Harshad

Tried it but without luck. With your version, the field will be shown correctly in listview and detailview but when edditing the data, the field is not read-only anymore. The only optionis which works is ‘type’ => ‘readonly’. :frowning:

Have you add this in metadata folder files?

Yes, but I gave it a try and added ‘type’ => ‘currency’ in the edit- and detailview. Strange behaviour:


So far the only way it seems to work halfways is in “custom/Extension/modules/Opportunities/Ext/Vardefs/ _override_sugarfield_opt_tcv_weighted_c.php”.

Hi, can you please let us know if it has got a solution. Thank you

Nope, it’s not working, as I mentioned in my previous poste. It results in a very strange behaviour, where the amount isn’t shown. Instead the title of the field is shown. See poste with screen shots above.

Thanks for the reply. I tried the following solution and worked for me. The steps are as follows:

  1. Create a file at
    extensions/defaultExt/modules/Opportunities/ViewDefinitions/Mappers/RecordViewReadonlyCurrencyFieldMapper.php
    with the following content. (Class name and file name should be the same).

Please note the field names which are basically the name of the currency field concatenated with ‘-group’ and set the readonly property. The row number is the row index from the detailviewdefs panel definitions.

<?php

namespace App\Extension\defaultExt\modules\Opportunities\ViewDefinitions\Mappers;


use App\FieldDefinitions\Entity\FieldDefinition;
use App\ViewDefinitions\Entity\ViewDefinition;
use App\ViewDefinitions\LegacyHandler\ViewDefinitionMapperInterface;

class RecordViewReadonlyCurrencyFieldMapper  implements ViewDefinitionMapperInterface
{
    public function __construct()
    {
    }
    public function getModule(): string
    {
        return 'opportunities';
    }

    public function getKey(): string
    {
        return 'opportunities-record-view-readonly-currency';
    }


    public function map(ViewDefinition $definition, FieldDefinition $fieldDefinition): void
    {
       $recordView = $definition->getRecordView() ?? [];
        $panels = $recordView['panels'] ?? [];
        $main = $panels[0] ?? [];
        $rows = $main['rows'] ?? [];
        $firstRow = $rows[1] ?? [];
        $cols = $firstRow['cols'] ?? [];
		
		foreach ($cols  as $cellKey => $cell) {
			 $cell = $cols[$cellKey];
			 if($cell['name'] === 'amount-group'){
				 $cell['readonly']=true;
				 $cols[$cellKey] = $cell;
			 }			 
		}

		$firstRow['cols'] = $cols;
        $rows[1] = $firstRow;
        $main['rows'] = $rows;
        $panels[0] = $main;
        $recordView['panels'] = $panels;
        $definition->setRecordView($recordView);
    }
}
  1. Run php/bin concole cache:clear to test
    In my example the opportunity amount is readonly.
1 Like

Hi @Harshad
Sorry, I am not yet so familiar with the file structure of SuiteCRM. In the folder “webroot/suitecrm/extensions/defaultExt” is no folder “modules”. Shall I create the folder structure there, according to you recommendations? Or, since it is a custom script, should it be located under “webroot/suitecrm/public/legacy/custom/…”

Sorry for the dump question.

Cheers,
Carsten

Yes please create the folder structure if it is not present.

Good morning @Harshad

Today, it’s not clicking for me. Maybe I need another coffee. hahaha…

So I uploaded the file, reset the readonly in the custom/Extension/modules/Opportunities/Ext/Vardefs/editviewdefs.php to normal. Then I did a Quick Repair and Restore and cleared the cache as requested.

Before, I tried to figure out the name of the field and row index thing you described, but I stuck at this point. Can you shortly give me a hint?

Here the code of my editviewdefs.php (which is the same like the detailviewdefs.php:

<?php
$viewdefs ['Opportunities'] = 
array (
  'EditView' => 
  array (
    'templateMeta' => 
    array (
      'maxColumns' => '2',
      'widths' => 
      array (
        0 => 
        array (
          'label' => '10',
          'field' => '30',
        ),
        1 => 
        array (
          'label' => '10',
          'field' => '30',
        ),
      ),
      'javascript' => '{$PROBABILITY_SCRIPT}',
      'useTabs' => false,
      'tabDefs' => 
      array (
        'DEFAULT' => 
        array (
          'newTab' => false,
          'panelDefault' => 'expanded',
        ),
        'LBL_EDITVIEW_PANEL1' => 
        array (
          'newTab' => false,
          'panelDefault' => 'expanded',
        ),
        'LBL_PANEL_ASSIGNMENT' => 
        array (
          'newTab' => false,
          'panelDefault' => 'collapsed',
        ),
      ),
      'syncDetailEditViews' => true,
    ),
    'panels' => 
    array (
      'default' => 
      array (
        0 => 
        array (
          0 => 
          array (
            'name' => 'opty_no_c',
            'label' => 'LBL_OPTY_NO_C',
          ),
          1 => 
          array (
            'name' => 'date_closed',
          ),
        ),
        1 => 
        array (
          0 => 
          array (
            'name' => 'name',
          ),
          1 => 
          array (
            'name' => 'opt_pro_start_c',
            'label' => 'LBL_OPT_PRO_START_C',
          ),
        ),
        2 => 
        array (
          0 => 'description',
          1 => 
          array (
            'name' => 'opt_pro_end_c',
            'label' => 'LBL_OPT_PRO_END_C',
          ),
        ),
        3 => 
        array (
          0 => 'account_name',
        ),
        4 => 
        array (
          0 => 
          array (
            'name' => 'opt_contract_type_c',
            'studio' => 'visible',
            'label' => 'LBL_OPT_CONTRACT_TYPE_C',
          ),
          1 => 
          array (
            'name' => 'opty_estimated_efforts_c',
            'label' => 'LBL_OPTY_ESTIMATED_EFFORTS_C',
          ),
        ),
        5 => 
        array (
          0 => 'sales_stage',
          1 => 
          array (
            'name' => 'opty_probability_c',
            'label' => 'LBL_OPTY_PROBABILITY_C',
          ),
        ),
        6 => 
        array (
          0 => 
          array (
            'name' => 'opty_lost_reason_c',
            'studio' => 'visible',
            'label' => 'LBL_OPTY_LOST_REASON',
            'display' => 'none',
            'displayLogic' => 
            array (
              'show_on_lost01' => 
              array (
                'key' => 'displayType',
                'modes' => 
                array (
                  0 => 'create',
                  1 => 'edit',
                  2 => 'detail',
                ),
                'params' => 
                array (
                  'fieldDependencies' => 
                  array (
                    0 => 'sales_stage',
                  ),
                  'activeOnFields' => 
                  array (
                    'sales_stage' => 
                    array (
                      0 => 
                      array (
                        'operator' => 'is-equal',
                        'values' => 
                        array (
                          0 => 'Closed Lost',
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
          1 => 
          array (
            'name' => 'opty_lost_description_c',
            'studio' => 'visible',
            'label' => 'LBL_OPTY_LOST_DESCRIPTION',
            'display' => 'none',
            'displayLogic' => 
            array (
              'show_on_lost02' => 
              array (
                'key' => 'displayType',
                'modes' => 
                array (
                  0 => 'create',
                  1 => 'edit',
                  2 => 'detail',
                ),
                'params' => 
                array (
                  'fieldDependencies' => 
                  array (
                    0 => 'sales_stage',
                  ),
                  'activeOnFields' => 
                  array (
                    'sales_stage' => 
                    array (
                      0 => 
                      array (
                        'operator' => 'is-equal',
                        'values' => 
                        array (
                          0 => 'Closed Lost',
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
      'lbl_editview_panel1' => 
      array (
        0 => 
        array (
          0 => 
          array (
            'name' => 'opt_tcv_c',
            'label' => 'LBL_OPT_TCV_C',
          ),
          1 => 
          array (
            'studio' => 'visible',
            'name' => 'opt_tcv_weighted_c',
            'label' => 'LBL_OPT_TCV_WEIGHTED_C',
          ),
        ),
        1 => 
        array (
          0 => 
          array (
            'name' => 'opt_tco_c',
            'label' => 'LBL_OPT_TCO_C',
          ),
          1 => 
          array (
            'studio' => 'visible',
            'name' => 'opt_tcv_100_c',
            'label' => 'LBL_OPT_TCV_100_C',
          ),
        ),
      ),
      'LBL_PANEL_ASSIGNMENT' => 
      array (
        0 => 
        array (
          0 => 
          array (
            'name' => 'rec_status_c',
            'studio' => 'visible',
            'label' => 'LBL_REC_STATUS',
          ),
          1 => 'assigned_user_name',
        ),
        1 => 
        array (
          0 => 
          array (
            'name' => 'created_by_name',
            'label' => 'LBL_CREATED',
          ),
          1 => 
          array (
            'name' => 'date_entered',
            'comment' => 'Date record created',
            'label' => 'LBL_DATE_ENTERED',
          ),
        ),
        2 => 
        array (
          0 => 
          array (
            'name' => 'modified_by_name',
            'label' => 'LBL_MODIFIED_NAME',
          ),
          1 => 
          array (
            'name' => 'date_modified',
            'comment' => 'Date record last modified',
            'label' => 'LBL_DATE_MODIFIED',
          ),
        ),
      ),
    ),
  ),
);
;
?>

We are talking about the two fields: opt_tcv_weighted_c and opt_tcv_100_c in the panel “lbl_editview_panel1”.

Which row and col shall I set in the mapper file?

Sorry for bothering you, but it seems, I am still on the Monday mood. :wink:

I think it is panel with index 1 and rows with indexes of 0 and 1 for your fields.
You will have to write logic for the two fields as shown in the example given above for ‘amount’ field. and the update the panels and record view definitions.

It works perfect! I copied the logic for the second field and it works like a charm!

Thank you @Harshad for your help.