Skip to main content

SugarCRM : Programmatically Hiding and Displaying Panels Based on the Value of a Drop Down

Sugar Version: 5.1

Overview

In many cases it is desirable to hide or show certain fields on a form based on the value of a field.  For example, if the account is a “partner” the form should show partnerish fields.  The easiest way to do this is to create panels with information specific to each record type and then hide or show the panels based on the record type.

Step 1 – Rearrange the Edit and Detail Views

Start with the edit view first.  From Studio, open the account edit view and create 2 panels.
Name the first panel “CUSTOMER INFO” and the second panel “PARTNER INFO”.   The naming of these is very important because they will be used in the JavaScript code to hide these panels.  Drag some fields into both panels (See example below)

Repeat the process for the detail view ensuring the panel names are the same.



Creating a Custom Edit Handler

Create a file called custom/modules/Accounts/views/view.edit.php.  This file is a custom edit view handler for the accounts module.  It will use all of the default behavior, but it will add some JavaScript code to add an event handler for the onchange event of the account type.  The text in red is what you would change for your specific field and panels.
Add the following code:
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

require_once('include/MVC/View/views/view.edit.php');
class AccountsViewEdit extends ViewEdit {
        function AccountsViewEdit(){
                parent::ViewEdit();
        }
        function display() {
               
                                global $app_list_strings;         
                                global $mod_strings;
                               
                                $fieldName = 'account_type'; //change this to the name of your field
                                $dropDownName = 'account_type_dom'; //change this to the name of the dropdown
                               
                                $e=$this->ev->defs['panels'];
                                $panelArray = array();
                                foreach($e as $panel_label=>$panel_data) {
                                                if($panel_label != '' && $panel_label != 'default')
                                                {
                                                                $tempArray = array($panel_label,$mod_strings[strtoupper($panel_label)]);
                                                                array_push($panelArray,$tempArray);
                                                }
                                }

                                $prePop = 'document.getElementById(\''.$fieldName.'\').onchange();';
                                                               
                                print '';                       

$js=<<
        
                               
EOQ;
                parent::display();
                echo $js;
        }
}
?>

Explaining the Code
class AccountsViewEdit extends ViewEdit {
        function AccountsViewEdit(){

These two lines set the class and function that is required in edit views. Change the text in red to the name of the module that you are configuring this for.
$fieldName = 'account_type'; //change this to the name of your field
$dropDownName = 'account_type_dom'; //change this to the name of the dropdown

So that the code can be easily adapted to any module, we assign the field that we are checking and the drop down list to variables that will be used throughout the program. The values in red need to be changed so that they reflect the fields in your module.
1 $e=$this->ev->defs['panels'];
2                              $panelArray = array();
3                              foreach($e as $panel_label=>$panel_data) {
4                                              if($panel_label != '' && $panel_label != 'default')
5                                              {
6                                                              $tempArray = array($panel_label,$mod_strings[strtoupper($panel_label)]);
7                                                              array_push($panelArray,$tempArray);
8                                              }
9                              }
 In 5.1 the div names are set with the system panel label which will be something similar toLBL_PANEL1. There are also corresponding user panel labels which are what is seen in the UI ( CUSTOMER INFO ).
Line 1 assigns the editview ( ev ) panel definitions ( defs[‘panels’]  ) to the variable $e.
The foreach loop iterates through all the system panel labels and checks to make sure that they aren’t empty or named default  ( Line 4 ). Then is pushes the system panel label onto an array with its corresponding user panel label ( Line 6 ). On line 7 we push the $tempArray of corresponding values on to another array called $panelArray. This allows me to iterate through $panelarray and get sets of data instead of one value.
//Code to hide or display the panels when first editing the record
 $prePop = 'document.getElementById(\''.$fieldName.'\').onchange();';

When the form first draws (as either a new record or editing an existing record) the correct panels should be hidden or displayed.  The code above will call the onchange event for the account type so the form is initially rendered properly.
print '';
                                                               
                                                               

$js=<<
        
EOQ;
        parent::display();
        echo $js; 
        }
}
?>


Comments

Popular posts from this blog

Install SugarCRM - Step by Step Guide

SugarCRM is the market-leading, commercial Open Source customer relationship management (CRM) application. SugarCRM’s Open Source architecture easily adapts to any business environment by offering a more flexible, cost-effective alternative than proprietary applications. It offers a complete CRM system for businesses of all sizes. For a small business SugarCRM can be a huge time and expense saver. It requires minimal technical knowledge to get it installed and the built in web administration console and backup system allow you to feel comfortable that it’s going to keep running. Core SugarCRM functionality includes sales automation, marketing campaigns, support cases, project mgmt, calendaring and more. Built in PHP, supports MySQL and SQL Server allows it that cool functionality LAMP system. We have tested it Windows XP/Vista/2k3 . Step 1: Installing XAMPP on Windows 1. Go to http://www.apachefriends.org/en/xampp-windows.html#641 and click "XAMPP Windows 1.7.1". Download...

STRESS MANAGEMENT

STRESS Stress is an unpleasant fact of life.  We all experience it for various reasons,  and we all try to come up with ways of  coping with it—some with more success  than others. So what exactly is stress  doing to your mind (and body)  when you're staring down a deadline?  And what can you do to power through it? What Stress Actually Does  to You and What You Can  Do About It The real problem with stress is that, for such a well  understood and universally experienced condition,  as a society we deal with it so poorly that it leads  to many of our most lethal illnesses and  long-term health problems. High blood  pressure, heart disease, cancer, stroke,  obesity, and insomnia are all medical  conditions across the spectrum that  can be related to or directly influenced  by high stress as an environmental conditio...

Jquery Carousels

Jquery Carousels we all love carousels they are a fantastic way to give the effects we want our visitors to have, more than that we have multiple components to show like images, links, text etc. There are lot many ways to achieve it, Jquery is ofcourse the best possible option available outside. We all search for lot of ems it can be Jquery Flexslider  or Jcarousel Lite  whatever you choose, customization is required, in this tutorial I am not going to focus on how to install these libraries rather one step ahead, to let you know how these carousels can be called multiple times in the same page having their controls working respectively for each carousel instance. So, lets get started with the HTML <div id="sideRight"> <div id="first">       <!-- Do not change the class and tag type, as this will remain as it is for all the following divisions-->       <p class="containheader">Plans for you <a ...