Subpanel with selectable Contacts of related Module

Hi there,

we use the Accounts module for storing companies (our customers). Accounts has a subpanel Contacts. Here the people are created with a category (selection list).
In another custom module I have added the company via a relationship. Now I want to enable in a subpanel the created contacts of the company for selection, which correspond to a certain category.

How is this possible?

@qivstephan

Welcome to community!

Yes, it’s possible. You should make button in custom directory for example you can use the file:
include/generic/SugarWidgets/SugarWidgetSubPanelTopSelectButton.php

@p.konetskiy

Thanks for your response. I’m not sure, if I got it right. I’ve copied SugarWidgetSubPanelTopSelectButton.php from the specified folder. Should it be placed in /custom/modules/[Modulname]/metadata/subpanels ?

Since I am completely new to suitecrm, I am a in need of help

@qivstephan
You should copy it to:
custom/include/generic/SugarWidgets/SugarWidgetSubPanelTopSelectButton.php
If you will use this the file name and class you will change SelectButton button for all modules.

If you want to make button for the one module you should set thenew file name and class. For example look at the file:
include/generic/SugarWidgets/SugarWidgetSubPanelTopSelectAccountButton.php. The button get in array accounts of file modules/Accounts/metadata/subpaneldefs.php
You should directory custom/include/generic/SugarWidgets/ for your buttons.

@p.konetskiy

Okay, than I got the missunderstanding.

Now I’v made a file SugarWidgetsLsContactsTopSelectButton.php in /custom/include/generic/SugarWidgets/ based on include/generic/SugarWidgets/SugarWidgetSubPanelTopSelectAccountButton.php

After that, I’ve created /custom/modules/[Modulname]/metadata/subpaneldefs.php

<?php
if (!defined('sugarEntry') || !sugarEntry) {
    die('Not A Valid Entry Point');
}

$layout_defs['Modulname'] = [
	'subpanel_setup' => [
		'contacts' => array(
            'order' => 30,
            'module' => 'Contacts',
            'sort_order' => 'asc',
            'sort_by' => 'last_name, first_name',
            'subpanel_name' => 'ForAccounts',
            'get_subpanel_data' => 'contacts',
            'add_subpanel_data' => 'contact_id',
            'title_key' => 'LBL_CONTACTS_SUBPANEL_TITLE',
            'top_buttons' => array(
                array('widget_class' => 'LsContactsTopSelectButton'),
            ),
        )
	]
];

And the subpanel is visible. If I click on the “Select” Button, the popup comes over with search for contacts. Now, since the contacts are related to an Account and Account is associated with my module, I want only all contacts be visible which are associated with the related Account.

@qivstephan

Look at the function display in file:
include/generic/SugarWidgets/SugarWidgetSubPanelTopSelectContactsButton.php
This file contains more information. You should set correct variable $initial_filter. The format is:

"&<field_name_of_select_module>_advanced=<field_name_of_current_module>"

I don’t know detail about configuration of your system. Maybe you can add addition array to your file /custom/modules/<Modulname>/metadata/subpaneldefs.php without changing select button. Look at:

<?php
if (!defined('sugarEntry') || !sugarEntry) {
    die('Not A Valid Entry Point');
}

$layout_defs['Modulname'] = [
	'subpanel_setup' => [
		'contacts' => array(
            'order' => 30,
            'module' => 'Contacts',
            'sort_order' => 'asc',
            'sort_by' => 'last_name, first_name',
            'subpanel_name' => 'ForAccounts',
            'get_subpanel_data' => 'contacts',
            'add_subpanel_data' => 'contact_id',
            'title_key' => 'LBL_CONTACTS_SUBPANEL_TITLE',
            'top_buttons' => array(
// standard select button
                array('widget_class' => 'SubPanelTopSelectButton'),
// addition the line
                      'initial_filter_fields' => array(<field_name_of_current_module> => <field_name_of_select_module>'_advanced')
            ),
        )
	]
];

@p.konetskiy

I am very grateful for your help! I’ll take a look at it right away.

While exploring it, I noticed the following: The module table has an attribute account_id in the database. I was fine with in case of the o:m relation. But it is always null. Instead, the relationship is mapped via separate table modulname_accounts_c with some _ida _idb fields. Is this right?

@qivstephan

If relationship one-to-many or one-to-one is using field with format name <name>_id
If relationship many-to-many is using the additional table.

@p.konetskiy

Yes, but it is set up as o:m. That’s the reason why I’m confused.

@qivstephan

Sorry. If you write more details I will answer something.