Pages

Thursday, April 19, 2012

SugarCRM Customization: Dynamic Subpanels


Suppose we are working on a SugarCRM instance for an organization that uses the Accounts module to store records representing either customers or suppliers. The distinction is made by selecting the appropriate value on the Account Type field of an Account record. 

Opportunities, or sales, are only relevant to customers, as suppliers would not be purchasing from the organization. Our task is to simplify the DetailView screen as much as possible by removing unnecessary components. 

Eliminating components from the view has the net effect of making it easier for users to quickly locate data they find most valuable, along with simplifying navigation. With this goal in mind, we conclude one helpful customization would be to only display the Opportunities subpanel on records that are of type Customer

How do we accomplish this?

It is actually rather simple. To apply this type of customization, we simply need to create a custom PHP file and execute a Quick Repair and Rebuild of our SugarCRM instance.

What does that custom PHP file look like?



It looks like the following:


<?php

/*************************************
Project: Subpanel Suppression
Original Dev: Angel Magaña, April 2012
@2012 Angel Magaña
cheleguanaco[at]cheleguanaco.com

Desc: Subpanel suppression based on record data

The contents of this file are governed by the GNU General Public License (GPL).
A copy of said license is available here: http://www.gnu.org/copyleft/gpl.html
This code is provided AS IS and WITHOUT WARRANTY OF ANY KIND.
*************************************/

if ($this->_focus->account_type != 'Customer')
{
unset($layout_defs['Accounts']['subpanel_setup']['opportunities']);
}

?>

Paste this code into a new file named OpptyPanel.php. Save this file in custom/Extension/modules/Accounts/Ext/Layoutdefs. Lastly, execute a repair of the system by selecting Admin > Repair > Quick Repair and Rebuild.

Verify it is working by accessing the DetailView of an Account record marked as type Customer and then accessing one marked as Supplier. If all went well, the Opportunities subpanel should only appear on the record marked as Customer.    

24 comments:

  1. Sounds interesting....i'll try this out.

    ReplyDelete
  2. hey angel can u explain how can the below situation be implemented....lets say

    contact is created(abc)...account is associated (xyz)
    when we create case(from case subpanel from contact module)....then account(xyz) will get displayed(pre-populated)
    so based on the accounts field say checkbox(feedback), display the text besides the relate field of accounts..."feedback-yes"(if the feedback field is checked) else "feedback-no" for unchecked.
    we wont show the text dyanamically.....like after selecting another account.....only first time during pre-populated after clicking create case.

    ReplyDelete
    Replies
    1. I am not entirely sure. I've never done anything like that. The closest to it that I have done is use custom JavaScript to validate a field upon one entering a value into it and then moving off that field.

      I suppose your first step would be to do that, inject custom JavaScript into the view in question.

      Delete
  3. Nice post angel. Now if only sugarlogic let me hide custom "panels" instead of just fields... I'd be all set.

    ReplyDelete
  4. blak3r, SugarLogic does allow you do that..

    ReplyDelete
  5. Angel, great post, this info was exactly what I was looking for. Thanks!

    ReplyDelete
  6. Angel, great! Many thanks, you made my day :-)

    ReplyDelete
  7. This works great! Was able to implement it to Cases.
    Though I have an issue: (Quite new with SugarCRM and not that good with codings)
    How can I customize your script so that I can apply this to multiple status.

    ReplyDelete
    Replies
    1. $this->_focus->status would give you the status of the current Case record. You should be able to use that build out your logic.

      Delete
    2. Thanks. Was able to make it work.

      Delete
  8. If you are still seeing the subpanel you are trying to hide, verify your changes are being written to this file:
    /custom/modules/MODULE_NAME/Ext/Layoutdefs/layoutdefs.ext.php

    I was making a mistake by writing layoutdefs instead of layout_defs.


    Thanks AGAIN Angel, your blog is most useful... nearly as much as the sugar forums. ;)

    ReplyDelete
    Replies
    1. Thanks, glad I could help. :)

      And thanks for underscoring the importance of $layout_defs vs. $layoutdefs. I've made that same mistake myself before.

      Delete
  9. Thanks for the nice tutorial, it is very useful...
    Instead of unsetting the unnecessary subpanels, would there be any way of showing all the subpanels on the right side of the page?
    Something like splitting the detail into two columns with all subpanels displayed in the right side column.

    ReplyDelete
    Replies
    1. Never done anything like that, but you would need a custom DetailView to make that happen. The related TPL file with the SMARTY and HTML code for that view would need to be customized, highly, in order to do what you want.

      Take a look at the developers manual at http://developer.sugarcrm.com for further information on creating custom views.

      Delete
  10. Thanks for your tips. It actually turned out to be much easier than I have thought. For those interested here is the solution:
    http://forums.sugarcrm.com/f22/two-columns-detail-view-84997/#post295150

    ReplyDelete
  11. Angel, Awesome post. it's help me lots thanks dude

    ReplyDelete
  12. Thanks for tips. How would you do reverse of unset function? For example, if you disable sub panel from admin and you would like to display sub panel only in one specific module?

    ReplyDelete
    Replies
    1. I would think something like this would work, using the example of needing put back the Opportunities subpanel on the Accounts module:

      $layout_defs['Accounts']['subpanel_setup']['opportunities'] = Array(
      'order' => 40,
      'module' => 'Opportunities',
      'subpanel_name' => 'ForAccounts',
      'sort_order' => 'desc',
      'sort_by' => 'date_closed',
      'get_subpanel_data' => 'opportunities',
      'add_subpanel_data' => 'opportunity_id',
      'title_key' => 'LBL_OPPORTUNITIES_SUBPANEL_TITLE',
      'top_buttons' => array(
      array('widget_class' => 'SubPanelTopButtonQuickCreate')
      );

      Note that the security code that suppresses it might be applied afterwards, so I am not sure if this would work.

      Another option would be to use the suppress method outlined in this post to hide the subpanel in all modules, except where you need it displayed.

      Delete
  13. Hi,

    I`m Juan Manuel,

    My english is not so good, sorry for this. I'm a new administrator of Sugar CRM and I need to do "Id_account" and "Id_contact" numerical and automated. Can you help me? I dont know what archive I need to change and what kind of change I need to change.

    thanks for attention.

    Regards

    ReplyDelete
  14. Hi Angel, great article ... for custom modules, where I want to make this a permanent feature of the module on deployment father than using the extension / Ext directory which then gets overwritten; where would I add new additions to the $layoutdefs? Is it in the builds or packages directory, and which file?

    Whatever I do seems to get overwritten when I redeploy the module. Thanks.

    ReplyDelete
    Replies
    1. Still struggling with this! i want to set custom sort order on subpanel built using module builder, but whenever I redeploy the module the file containing the $layout_defs just gets overwritten. Thanks anyone thats able to help?

      Delete
  15. I wish people would post solutions as well as questions so we can all learn :(

    ReplyDelete

Your comments, feedback and suggestions are welcome, but please refrain from using offensive language and/or berating others. Thank you in advance.