Display field (relate type) with concatenated multiple fileds from related table

Greetings, community! I am PHP programmer, new to SuiteCRM.

I have created the CNAE module:

Every account might have one CNAE, for that I created a field (relate type) in the Account:

I want to display this related filed on all Account’s view (details, create, edit, list, etc) with these CNAE’s fields in the following format:

"{$subclasse_id} {$subclasse} {$classe} {$grupo} {$divisao} {$secao}"

How can I achieve that?

Have a look at posts containing “customCode”, these are about customizing viewdefs to show fields like what you describe

@pgr I did that… no luck

Here is what I tried, but it’s returning error:

public/legacy/custom/modules/Accounts/metadata/listviewdefs.php:

<?php

$listViewDefs ['Accounts'] = array (
  
  //...

  'CNAE_PRIMARIO_C' => array (
    'type' => 'relate',
    'default' => true,
    'studio' => 'visible',
    'label' => 'LBL_CNAE_PRIMARIO',
    'id' => 'CNAE_CNAE_ID_C',
    'link' => true,
    'width' => '10%',
    "customCode" => "{$SUBCLASSE_ID} {$SUBCLASSE} {$CLASSE} {$GRUPO} {$DIVISAO} {$SECAO}"
  )
);

?>

public/legacy/custom/modules/Accounts/metadata/detailviewdefs.php:

<?php

$viewdefs ['Accounts'] = 
array (
  'DetailView' => 
  array (
    'panels' => 
    array (
      'lbl_account_information' => 
      array (

        // ...
        
        6 => 
        array (
          0 => 
          array (
            'name' => 'cnae_primario_c',
            'studio' => 'visible',
            'label' => 'LBL_CNAE_PRIMARIO',
            "customCode" => "{$fields.subclasse_id.value} {$fields.subclasse.value} {$fields.classe.value} {$fields.grupo.value} {$fields.divisao.value} {$fields.secao.value}"
          ),
        ),
      ),
    ),
  ),
);

?>

You’re probably not referencing your fields correctly…

Try looking at them in Studio, for example, the field name in your screenshot above is cnae_primario_c

EDIT: I just asked ChatGPT

The idea is to run this once to see full list of all the fields available to you. Then you can choose the correct names.

@pgr I did a quick test using the code provided by ChatGPT

public/legacy/custom/modules/Accounts/metadata/detailviewdefs.php:

// ...

array (
    'name' => 'name',
    'comment' => 'Name of the Company',
    'label' => 'LBL_NAME',
    "customCode" => 
        "<ul>
            {foreach from=$fields key=name item=value} 
                <li><strong>{$name}:</strong> {$value}</li>
            {/foreach}
        </ul>"
),

But it’s generating this warning Undefined variable $fields

logs/prod/prod.log:

[2024-10-17 23:55:19] php.WARNING: Warning: Undefined variable $fields {"exception":"[object] (ErrorException(code: 0): Warning: Undefined variable $fields at /var/www/public/legacy/custom/modules/Accounts/metadata/detailviewdefs.php:206)"} []
[2024-10-17 23:55:19] php.WARNING: Warning: Undefined variable $name {"exception":"[object] (ErrorException(code: 0): Warning: Undefined variable $name at /var/www/public/legacy/custom/modules/Accounts/metadata/detailviewdefs.php:207)"} []
[2024-10-17 23:55:19] php.WARNING: Warning: Undefined variable $value {"exception":"[object] (ErrorException(code: 0): Warning: Undefined variable $value at /var/www/public/legacy/custom/modules/Accounts/metadata/detailviewdefs.php:207)"} []

Are these warnings displayed on the screen?

Try this one:

array (
    'name' => 'name',
    'comment' => 'Name of the Company',
    'label' => 'LBL_NAME',
    "customCode" => 
        "<ul>
            {if isset(\$fields) && count(\$fields) > 0}
                {foreach from=\$fields key=name item=value} 
                    <li><strong>{\$name}:</strong> {\$value}</li>
                {/foreach}
            {else}
                <li>No fields available.</li>
            {/if}
        </ul>"
),

I’m lost.

If I were you I would get a stack trace of those errors, set up my debugger and put a breakpoint on the line where that happens, then work up to the stack to the calling code, to see what assigns are being done before processing the Smarty template (that customCode is a Smarty template).

@rsp No. Is there a way to make it disaplayable on the screen?

Your code is not working, but at least now there is no meaningfull warning in the logs anymore.

By the way, I am using the Version 8.7.0 and I just read that customCode doesn’t work in version 8. So how to change the way a field is displayed in the version 8?

@pgr Thank you, I will try to setup a debugger later :slight_smile:

The division of what works or does not work is not simply between v7/v8.

v7 still lives inside v8, and any legacy modules will use customCode. And you can control which modules use legacy (search these forums for module_routing.yaml)