Custom Subpanel sorting bug

I was able to add new subpanels that handles only a specific type of opportunities by following this link.

My problem is after creating, when I try to sort it. It shows No Result afterwards and needs to be refreshed.

This is my code.
root/custom/modules/Accounts/customOpportunitiesSubpanel.php

<?php 
function get_opportunities_type($params) {
    $args = func_get_args();
    $servicetype_c = $args[0]['servicetype_c'];
    $accountId = $args[0]['account_id'];
    $return_array['select'] = " SELECT opportunities.*, opportunities_cstm.*, account.id AS accounts_opportunities";
    $return_array['from'] = " FROM opportunities, opportunities_cstm ";
    $return_array['join'] = " 
    	INNER JOIN accounts_opportunities ON accounts_opportunities.opportunity_id = opportunities.id 
    		AND accounts_opportunities.deleted = '0'
		INNER JOIN accounts ON accounts.id = accounts_opportunities.account_id AND accounts.deleted = '0'
			AND accounts.id = '{$accountId}'";
    $return_array['where'] = " WHERE opportunities_cstm.id_c=opportunities.id AND opportunities_cstm.servicetype_c='{$servicetype_c}'";
    $return_array['join_tables'] = '';
    return $return_array;
}

root/custom/modules/Accounts/customOpportunitiesSubpanel.php

<?php 
$layout_defs['Accounts']['subpanel_setup']['publishing'] =
    array('order' => 49,
    'module' => 'Opportunities',
    'subpanel_name' => 'Account_subpanel_opportunities',
    'get_subpanel_data' => 'function:get_opportunities_type',
    'generate_select' => true,
    'title_key' => 'LBL_OPPORTUNITIES_PUBLISHING',
    'top_buttons' => array(),
    'function_parameters' => array(
        'import_function_file' => 'custom/modules/Accounts/customOpportunitiesSubpanel.php',
        'servicetype_c' => 'Publishing',
        'account_id' => $this->_focus->id,
        'return_as_array' => 'true'
    ),
    'id' => 'publishing_opportunities_panel',
);
$layout_defs['Accounts']['subpanel_setup']['marketing'] =
    array('order' => 50,
    'module' => 'Opportunities',
    'subpanel_name' => 'Account_subpanel_opportunities',
    'get_subpanel_data' => 'function:get_opportunities_type',
    'generate_select' => true,
    'title_key' => 'LBL_OPPORTUNITIES_MARKETING',
    'top_buttons' => array(),
    'function_parameters' => array(
        'import_function_file' => 'custom/modules/Accounts/customOpportunitiesSubpanel.php',
        'servicetype_c' => 'Marketing',
        'account_id' => $this->_focus->id,
        'return_as_array' => 'true'
    ),
    'id' => 'marketing_opportunities_panel',
);
$layout_defs['Accounts']['subpanel_setup']['addon'] =
    array('order' => 50,
    'module' => 'Opportunities',
    'subpanel_name' => 'Account_subpanel_opportunities',
    'get_subpanel_data' => 'function:get_opportunities_type',
    'generate_select' => true,
    'title_key' => 'LBL_OPPORTUNITIES_ADDON',
    'top_buttons' => array(),
    'function_parameters' => array(
        'import_function_file' => 'custom/modules/Accounts/customOpportunitiesSubpanel.php',
        'servicetype_c' => 'Addon',
        'account_id' => $this->_focus->id,
        'return_as_array' => 'true'
    ),
    'id' => 'addon_opportunities_panel',
);

?>

I only copied the original opportunities subpanelā€™s columns from this:
root/custom/modules/Opportunities/metadata/subpanels

<?php
// created: 2021-09-01 15:57:21
$subpanel_layout['list_fields'] = array (
  'rmproject_code_c' => 
  array (
    'type' => 'autoincrement',
    'default' => true,
    'vname' => 'LBL_RMPROJECT_CODE',
    'width' => '10%',
  ),
  'account_name' => 
  array (
    'type' => 'relate',
    'link' => true,
    'vname' => 'LBL_ACCOUNT_NAME',
    'id' => 'ACCOUNT_ID',
    'width' => '10%',
    'default' => true,
    'widget_class' => 'SubPanelDetailViewLink',
    'target_module' => 'Accounts',
    'target_record_key' => 'account_id',
  ),
  'opp_name_c' => 
  array (
    'type' => 'enum',
    'default' => true,
    'studio' => 'visible',
    'vname' => 'LBL_OPP_NAME',
    'width' => '10%',
  ),
  'booktitle_c' => 
  array (
    'type' => 'varchar',
    'default' => true,
    'vname' => 'LBL_BOOKTITLE',
    'width' => '10%',
  ),
  'name' => 
  array (
    'name' => 'name',
    'vname' => 'LBL_LIST_OPPORTUNITY_NAME',
    'widget_class' => 'SubPanelDetailViewLink',
    'width' => '40%',
    'default' => true,
  ),
  'sales_stage' => 
  array (
    'name' => 'sales_stage',
    'vname' => 'LBL_LIST_SALES_STAGE',
    'width' => '15%',
    'default' => true,
  ),
  'project_status_c' => 
  array (
    'type' => 'enum',
    'default' => true,
    'studio' => 'visible',
    'vname' => 'LBL_PROJECT_STATUS',
    'width' => '10%',
  ),
  'date_closed' => 
  array (
    'name' => 'date_closed',
    'vname' => 'LBL_LIST_DATE_CLOSED',
    'width' => '15%',
    'default' => true,
  ),
  'amount_usdollar' => 
  array (
    'vname' => 'LBL_LIST_AMOUNT_USDOLLAR',
    'width' => '15%',
    'default' => true,
  ),
  'invoiceno_c' => 
  array (
    'type' => 'varchar',
    'default' => true,
    'vname' => 'LBL_INVOICENO',
    'width' => '10%',
  ),
  'fulfillment_prio_level_c' => 
  array (
    'type' => 'enum',
    'default' => true,
    'studio' => 'visible',
    'vname' => 'LBL_FULFILLMENT_PRIO_LEVEL',
    'width' => '10%',
  ),
  'assigned_user_name' => 
  array (
    'name' => 'assigned_user_name',
    'vname' => 'LBL_LIST_ASSIGNED_TO_NAME',
    'widget_class' => 'SubPanelDetailViewLink',
    'target_record_key' => 'assigned_user_id',
    'target_module' => 'Employees',
    'width' => '10%',
    'default' => true,
  ),
  'aro_assigned_c' => 
  array (
    'type' => 'relate',
    'default' => true,
    'studio' => 'visible',
    'vname' => 'LBL_ARO_ASSIGNED',
    'id' => 'USER_ID1_C',
    'link' => true,
    'width' => '10%',
    'widget_class' => 'SubPanelDetailViewLink',
    'target_module' => 'Users',
    'target_record_key' => 'user_id1_c',
  ),
  'payment_status_c' => 
  array (
    'type' => 'enum',
    'default' => true,
    'studio' => 'visible',
    'vname' => 'LBL_PAYMENT_STATUS',
    'width' => '10%',
  ),
  'date_modified' => 
  array (
    'type' => 'datetime',
    'vname' => 'LBL_DATE_MODIFIED',
    'width' => '10%',
    'default' => true,
  ),
  'edit_button' => 
  array (
    'vname' => 'LBL_EDIT_BUTTON',
    'widget_class' => 'SubPanelEditButton',
    'module' => 'Opportunities',
    'width' => '4%',
    'default' => true,
  ),
  'currency_id' => 
  array (
    'usage' => 'query_only',
  ),
);

What seems to be the problem here?
Thanks

This happened to me once. The issue is in the layout_def in your function_parameters array. For some reason the $this->_focus->id sometimes is empty.

My solution was to replace $this->_focus->id for $_REQUEST[ā€˜recordā€™] which seems to work everytime.

I hope this resolves your problem properly :slight_smile:

@Pablonr thatā€™s very interesting.

If you have a debugger set up, it would be nice to place a breakpoint inside the vardefs and catch the several places from which it is being executed. This would allow us to figure out why the context is different (works some times, others not).

Using REQUEST might work all the time, but itā€™s not the best of practices - we really shoulndā€™t be dependent on it, as its use is a bit chaotic.

Anyway, since the current v7 code is not going to change much, I guess the REQUEST solution is more than good enough for practical purposes.

Hi pgr,

Yes I have XDebugger configured in my local environment. When I have some more time I could go a bit more deep into the code to try figuring why the id is being retrieved only sometimes.

When I was testing I found that the id was empty when trying to order the subpanel by some column and the first time you try to open the subpanel. Doing any of those actions made the subpanel load empty. Then reloading the hole page with the subpanel opened made it load correctly

1 Like

Finally! Thanks @Pablonr, Iā€™ve been debugging this but cannot find a proper resolution. I have another question though, I have an Account Name column in that subpanel. BUT the name is not showing on the customs subpanels the same way that it works in the Opportunities Subpanel. Why is it?

Whatā€™s the subpanel showing exactly?

@pgr Iā€™ve debugged this to see whatā€™s happening exactly.
The problem is in include/Subpanel/SubpanelViewer.php. In line 86 thereā€™s this line:

$bean = BeanFactory::getBean($module);

Then the id isnā€™t set to the bean so the values for the record are never retrieved for this bean.
The workflow for loading the subpanel when loading the entire page is completely different and it should work fine everytime for this case.

Okā€¦ and can you make it work for both cases?

Maybe in that line, check if you have an ID available and use it?

This would need thorough testing, since ListView code is used in many different kinds of screens, including dashlets, etc.

This is what is showing:

I see the PR is already created :muscle:

Here it is, for reference :point_up:

Please apply that one line of code mentioned in the PR and let us know!

FILE:

include/SubPanel/SubPanelViewer.php

CODE: Line 86

$bean = BeanFactory::getBean($module, $record);